如何使用 Cucumber-Capybara 断言 flash 变量不存在?

发布于 2024-12-03 14:04:14 字数 714 浏览 0 评论 0原文

我正在使用 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

当我运行 Cucumber 测试时,出现错误

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],会发生相同类型的错误。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぃ双果 2024-12-10 14:04:15

(任何类型的)flash 消息集,您可以执行以下操作:

assert_predicate flash, :empty?

要断言不存在 特定类型集(本例中为 :error),您可以执行以下操作:

assert_predicate flash[:error], :nil?

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?
长不大的小祸害 2024-12-10 14:04:14

您应该检查页面上实际可见的内容,例如在本例中:

page.should_not have_css('.flash')

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

page.should_not have_css('.flash')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文