I'm trying to solve what is most likely a quick problem...
I have this so far
page.check 'Vendor'
page.check 'Partner'
click_button 'Invite'
newUser = User.find_by_email("lemmy#dl.com")
puts "ROLES"
puts newUser.roles
puts "ROLES"
expect(newUser.roles).to have_content("partner")
expect(newUser.roles).to have_content("vendor")
and what this outputs is :
ROLES
admin
vendor
partner
ROLES
and then an error:
expected to find text "partner" in "#"
I was wondering why this is, and although I know that the partner value is there since I printed it, I would like a way to automatically check it so i can get rid of those puts. Thank you for any help!
have_content looks at the web page to see if it has the content but you are inspecting newUser.roles instead. If newUser.roles returns an array use the include matcher instead:
expect(newUser.roles).to include("partner")
expect(newUser.roles).to include("vendor")
Related
In my create action in the comments controller I want to check if the comment text contains a "# + username" so I can add it to my public activities.
if #comment.body.include?(' [HERE] ')
maybe User.all.username.any? ?
You better use ruby regex to first find the user name from comment body matching pattern #abc and then user that username query database to find user
#comment.body.scan(/#(\w+)/).flatten.to_a.each do|username|
if User.where(:username => username).any?
# your code here
end
end
or search the users with single database query by passing the array
usernames = #comment.body.scan(/#(\w+)/).flatten.to_a
if User.where(:username => usernames).any?
# your code here
end
This will optimize the db query and will not load all users to memory.
A quick answer:
if User.pluck(:username).any? {|u| #comment.body.include?("##{u}")}
# ...
end
A better answer:
Why not use a javascript library to auto-complete the username? This is more user friendly, and scales better as the User table grows.
After updating capybara to 2.1, It is throwing Ambiguous error
Capybara::Ambiguous:
Ambiguous match, found 2 elements matching css "div.alert"
and after reading some articles and the log here. You can get the default behaviour from 1.0x by passing :match => :prefer_exact
This can be easily done for something like fill, like this
fill ("Password", :match => :prefer_exact'
but how do you do it for a within block like this
within 'div.alert' do
page.should have_content(text)
end
I tried doing this, but it throws an error
within ('div.alert', :match => :prefer_exact) do
page.should have_content(text)
end
any suggestions would be helpful.
If you don't know the order and have no way to select it by CSS, you can try
page.should have_selector('.alert', text: 'foo bar')
There is no need for within, which is designed for larger divs with complex content.
I have a model and a method inside that defined as follows :
class Farm < ActiveRecord::Base
def flags
#has other code which returns a flag_details hash eg:
# flag_details['flag1'] = true;
# flag_details['flag2'] = false;
end
end
Now I need to write a test to verify that a particular div is displayed based on the flags that are set/unset. And I want to write a stub to be able to return these flags and then test if the page is displaying the correct div. How do I correct the following code, to get what I intend:
scenario "display div named flower when flag1 or flag2 is false" do
farm.stub(:flags).and_return("flag1" => false, "flag2" => false)
if !farm.flags['flag1'] || !farm.flags['flag2']
expect(page).to have_selector('div.flower', text: "4" )
end
end
I am a beginner in ruby, rails and rspec, so any help will be great. I also tried using the following method but it did not work:
farm.stub(:flags[]).with('flag1').and_return(false)
farm.stub(:flags[]).with('flag2').and_return(false)
I also checked this documentation (https://www.relishapp.com/rspec/rspec-mocks/v/2-4/docs/method-stubs) but did not get my answer. Any other links that could be helpful in this, are really appreciated!
Thanks!
I think you should just be able to wrap that return value in curly braces ({}) and it should solve your problem:
farm.stub(:flags).and_return({"flag1" => false, "flag2" => false})
First, get rid of the if conditional in your test. There is no reason to add control flow to a test. How stubbing works: Your test needs to call the flags method on that farm object and it will return the hash that you have specified. Have your scenario visit the page in question and then check to make sure the expectation is behaving as you have intended.
Hi people as all know rails by default put ids and names id=model_attribute and name=model[attribute].
My question is How I can get the default id or name from a Model o with I18n?
For example User.human_attribute_name 'password' give the Password label, well how i do to put
User.give_me_the_id_or_name 'password' and give 'user[password]' or 'user_password'
Thinks at advance ^^
Look at the source. http://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/tags/base.rb
and build your own construction in your model class if you really want to. :)
def self.give_me_the_id_or_name(method)
result = "#{self.model_name.param_key}[#{method.to_s.sub(/\?$/,"")}]"
result.instance_eval do
def id
self.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
end
end
result
end
User.give_me_the_id_or_name(:password) ===> "user[password]"
User.give_me_the_id_or_name(:password).id ===> "user_password"
But I'm actually don't understand what you mean about I18n? These names used by default as tags titles when you rendering the model into tempate.
Cucumber generates out some neat webrat regex steps. I encountered a problem when I tried the this.
In feature:
And I fill in "Telephone (Home)" with "61234567"
In webrat steps:
When /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
fill_in(field, :with => value)
end
The error encountered:
Could not find field: "Telephone (Home)" (Webrat::NotFoundError)
It seems that the parenthesis between "Home" is giving problem. How do I tweak the regex to account for parenthesis?
UPDATE:
It seems that the regex wasn't the problem as the "field" instance variable did yield "Telephone (Home)". The real problem was the way webrat's "fill_in" method parses the field variable.
If you only want to capture "Telephone" try this:
/^I fill in "(\w+).*?" with "([^\"]*)"$/
If it's "Home" you're after try this:
/^I fill in "(?:.*?\()?(.+?)\)?" with "([^\"]*)"$/;
This encountered me too with the field "(log out)"...
You could call for the id field?
fill_in("user_telephone_home", :with => data)
I had a similar problem with matching labels to fields in webrat, and I came up with this code snippet which loosens the regexp used to match a label to a field. Maybe it will help you out.
I have this in my features/support/env.rb
module Webrat
module Locators
class FieldLabeledLocator < Locator
def matching_label_elements_with_numbering
label_elements.select do |label_element|
text(label_element) =~ /^.*#{Regexp.escape(#value.to_s)}.*$/i
end
end
alias_method_chain :matching_label_elements, :numbering
end
end
end
http://gist.github.com/169215