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

本小妞迷上赌 提交于 2019-12-10 15:53:46

问题


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
  undefined 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.


回答1:


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

page.should_not have_css('.flash')



回答2:


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?


来源:https://stackoverflow.com/questions/7357742/how-do-i-assert-nil-presence-of-flash-variables-with-cucumber-capybara

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!