【问题标题】:How do I assert the unexistance of a model in Cucumber?如何断言 Cucumber 中不存在模型?
【发布时间】:2012-10-02 18:56:38
【问题描述】:

如何断言在使用 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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 只是好奇,如果我们想要更细化并测试用户根本不输入任何输入的场景怎么办。在这种情况下,我们将无法使用User.where(email: ...),因为没有提供电子邮件地址。
    • ismaelga 的回答是进行该测试的好方法。您也可以存储 @user_count 并在以后进行测试。
    【解决方案2】:

    你也可以

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-03
        • 1970-01-01
        • 1970-01-01
        • 2023-01-07
        • 2021-12-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多