rspec .reload没有重新加载对象

时间:2012-06-03 16:48:03

标签: ruby-on-rails ruby ruby-on-rails-3 rspec

我已将以下测试添加到spec / requests / user_pages_spec.rb:

require 'spec_helper'

describe "UserPages" do
  subject { page }
  .
  .
  .
  describe "user should be able to update username" do
    before do
      @user = FactoryGirl.create(:user)
      visit new_user_session_path
      fill_in "Email", with: user.email
      fill_in "Password", with: user.password
      click_button "Sign In"
    end

    specify { @user.username.should_not == "New Name" }

    context "after filling in correct details" do
      before do
        visit edit_user_registration_path
        fill_in "Name", with: "New Name"
        fill_in "Current password", with: @user.password
        click_button "Update"
      end

      specify { @user.reload.username.should == "New Name" }
      it { should have_selector('div.alert.alert-notice', text: "You updated your account successfully." ) }
      it { should have_selector('title', text: "Edit account details" ) }
    end
  end

end

第一个指定返回用户名" Person",但重新加载后的第二个指定也是如此:

1) UserPages user should be able to update username after filling in correct details 
   Failure/Error: specify { @user.reload.username.should == "New Name" }
     expected: "New Name"
          got: "Person" (using ==)
   # ./spec/requests/user_pages_spec.rb:83:in `block (4 levels) in <top (required)>'

如果我更改任何一行,则测试失败,并且我已手动检查了应用并更新了工作。在重新获得名称之前,我还尝试过调用@user.reload

可能是什么问题?我应该将此代码放在其他地方吗?

感谢您的时间。

1 个答案:

答案 0 :(得分:2)

您应该在此行中使用@user个实例:

fill_in "Email", with: user.email
fill_in "Password", with: user.password

所以,它应该是:

fill_in "Email", with: @user.email
fill_in "Password", with: @user.password