How do I assert nil presence of flash variables wi

2019-06-26 06:44发布

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.

2条回答
forever°为你锁心
2楼-- · 2019-06-26 07:01

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

page.should_not have_css('.flash')
查看更多
我只想做你的唯一
3楼-- · 2019-06-26 07:10

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?
查看更多
登录 后发表回答