0

我对黄瓜很陌生。我正在尝试编写测试以确保用户不会创建重复的标签。于是写了一个功能,内容如下:

Scenario: Analyst adds a duplicate privacy tag
  Given I have a privacy tag called "Green"
  When I try to create a tag called "Green"
  Then I should be on the new privacy tag page
  And I should see an error message for "Green"

我的步骤是这样定义的:

Given /^I have a privacy tag called "(.*?)"$/ do |tag|
    PrivacyTag.create(:content => tag)
end

When /^I try to create a tag called "(.*?)"$/ do |arg1|
    visit new_privacy_tag_path
    fill_in 'privacy_tag[content]', :with => arg1
    click_button 'Create'
end

Then /^I should be on the new privacy tag page$/ do
    new_privacy_tag_path
end

Then /^I should see an error message for "(.*?)"$/ do |arg1|
    page.should have_selector('div', :class => 'alert alert-error') do |flash|
        flash.should have_content fartknocker
    end
end

所以奇怪的是,所有这些测试现在都通过了。当您尝试创建应用程序允许的重复隐私标签并最终进入隐私标签索引页面时,用户不会返回到 new_privacy_tag_path。但测试仍然通过。Cucumber 甚至连眼睛都不眨一下,因为在任何地方都没有定义名为 fartknocker 的变量,而且 fartknocker 这个词也没有出现在页面上的任何地方。测试仍然通过。是什么赋予了?

4

2 回答 2

0

那是因为page.should have_selector不占用块。您可能正在寻找的是within方法:

page.within('div', :class => 'alert alert-error') do
    page.should have_content fartknocker
end

编辑:由于在您的代码中,该行flash.should have_content fartknocker位于永远不会运行的块内,因此 Ruby 从不评估它,因此没有机会抱怨缺少的变量。

于 2012-08-29T17:38:27.980 回答
0
Then /^I should be on the new privacy tag page$/ do
  new_privacy_tag_path
end

这没有任何断言。所以它永远是绿色的。

Then /^I should see an error message for "(.*?)"$/ do |arg1|
  page.should have_selector('div', :class => 'alert alert-error') do |flash|
    flash.should have_content fartknocker
  end
end

你也必须小心have_content/matches/"equals". Jon M 在他的回答中提到的是您需要解决的问题。

于 2012-08-29T19:52:21.103 回答