1

我正在编写用于创建用户帐户的集成测试。

describe "with valid information" do
  before (:each) do
    fill_in 'first name',             :with => 'test'
    fill_in 'last name',              :with => 'user'
    fill_in "email",                  :with => "test@shakeshack.com"
    fill_in "password",               :with => "1234567"
    fill_in "password confirmation",  :with => "1234567"
  end

  it "should create a user" do
    expect {click_button "Create"}.to change(User, :count).by(1)

  end
end

当我运行本地服务器并使用相同的信息填写上述字段时,用户帐户已成功创建。我得到的错误如下:

User pages signup with valid information should create a user
 Failure/Error: expect {click_button "Create"}.to change(User, :count).by(1)
   count should have been changed by 1, but was changed by 0
 # ./spec/requests/users_spec.rb:43:in `block (4 levels) in <top (required)>'

提前感谢您的解决方案或建议!

4

1 回答 1

3

一些建议和要寻找的东西:

  • 您确定在浏览器中测试时已成功创建用户吗?您是否在 Rails 控制台或其他方式中进行了验证?
  • 您确定这些数据足以成功创建用户吗?
  • 在这种情况下,我通常会在某处发生静默失败,可能是由于未使用 bang 方法(例如save!)或未正确检查非 bang 方法的返回值(例如save)。

在测试期间,您始终可以save_and_open_page在规范中使用 capybara 来查看浏览器中的页面渲染:

describe "with valid information" do
  before (:each) do
    fill_in 'first name',             :with => 'test'
    fill_in 'last name',              :with => 'user'
    fill_in "email",                  :with => "test@shakeshack.com"
    fill_in "password",               :with => "1234567"
    fill_in "password confirmation",  :with => "1234567"
    save_and_open_page
  end

  it "should create a user" do
    expect {click_button "Create"}.to change(User, :count).by(1)

  end
end
于 2013-01-12T21:12:12.240 回答