如何在Cucumber中断言模型的存在?

时间:2012-10-02 18:56:39

标签: ruby-on-rails ruby testing rspec cucumber

如何断言使用Cucumber时未创建用户?在RSpec中,我会使用以下块,我无法将其转换为Cucumber:

...
describe "with invalid information" do
  it "should not create a new user" do
    expect { click_button submit }.not_to change(User, :count)
  end
end

# Cucumber equivalent
When /^he submits invalid information$/ do
  click_button "Sign up"
end
Then /^an account should not be created$/ do
  # not sure how to translate the expect block
end

3 个答案:

答案 0 :(得分:2)

在此示例中,您将了解用户将使用的电子邮件,因此您可以:

Then /^an account should not be created$/ do
  User.where(email: "youremail@example.com").first.should be_nil
end

答案 1 :(得分:2)

您也可以

Given I have 3 users
When I submit with invalid information
Then I should have 3 users

答案 2 :(得分:1)

您可以使用lambda直接翻译您的RSpec测试,而不是依赖于解决方法。

Then /^an account should not be created$/ do
  save = lambda { click_button :submit }
  expect(save).not_to change(User, :count)
end