开发者

How do I assert nil presence of flash variables with Cucumber-Capybara?

开发者 https://www.devze.com 2023-04-03 13:15 出处:网络
I\'m running a Rails 3.0.7 project with Cucumber and Capybara and I have a step definition that checks if a flash[:error] exists:

I'm running a Rails 3.0.7 project with Cucumber and Capybara and I have a step definition that checks if a flash[:error] exists:

Then /^I should be able to see the main page properly$/ do
    current_path.should == "/"
    flash[:error].should == nil
end

When I run my cucumber test, I get an error

And I should be able to see the main page properly # features/step_definitions/user_auth_steps.rb:17
  un开发者_开发问答defined method `flash' for nil:NilClass (NoMethodError)
  ./features/step_definitions/user_auth_steps.rb:19:in `/^I should be able to see the main page properly$/'
  features/user_auth.feature:10:in `And I should be able to see the main page properly'

Is this the correct way of handling variable assertions? I've noticed that if I were to use sessions[:something], the same type of error happens.


You should be checking what's actually visible on the page, e.g. in this case:

page.should_not have_css('.flash')


To assert that there is no flash message set (of any type), you can do:

assert_predicate flash, :empty?

To assert that there is no flash message of a particular type set (:error in this example), you can do:

assert_predicate flash[:error], :nil?
0

精彩评论

暂无评论...
验证码 换一张
取 消