【问题标题】:How do I assert nil presence of flash variables with Cucumber-Capybara?如何使用 Cucumber-Capybara 断言 Flash 变量不存在?
【发布时间】:2011-09-09 06:06:35
【问题描述】:

我正在使用 Cucumber 和 Capybara 运行 Rails 3.0.7 项目,并且我有一个检查是否存在 flash[:error] 的步骤定义:

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

当我运行我的黄瓜测试时,我得到一个错误

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'

这是处理变量断言的正确方法吗?我注意到,如果我要使用 session[:something],则会发生相同类型的错误。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 cucumber capybara web


    【解决方案1】:

    您应该检查页面上实际可见的内容,例如在这种情况下:

    page.should_not have_css('.flash')
    

    【讨论】:

      【解决方案2】:

      要断言没有 flash 消息集(任何类型),你可以这样做:

      assert_predicate flash, :empty?
      

      要断言没有特定类型集的flash 消息(本例中为:error),您可以这样做:

      assert_predicate flash[:error], :nil?
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-11
        • 1970-01-01
        • 2014-07-03
        • 1970-01-01
        • 2011-10-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多