RoR,Rspec测试神秘失败

时间:2013-01-12 20:49:51

标签: ruby-on-rails ruby testing rspec

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

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)>'

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

1 个答案:

答案 0 :(得分:3)

要寻找的一些建议和事项:

  • 在浏览器中测试后,您确定用户是否已成功创建?你在Rails控制台或其他方式验证了吗?
  • 您确定这是否足以成功创建用户?
  • 在这样的情况下我经常遇到的是某个地方的沉默失败,可能是因为没有使用爆炸方法(例如save!)或没有正确检查非爆炸方法的返回值(例如save)。

您可以随时在规范中使用capybara的save_and_open_page在测试期间在浏览器中查看页面的呈现:

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