1

我在 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在再次获得名字之前打电话。

可能是什么问题呢?我应该把这段代码放在别的地方吗?

谢谢你的时间。

4

1 回答 1

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
于 2012-06-03T18:32:18.743 回答