RSpec如何重新加载?

时间:2012-12-28 20:19:45

标签: ruby-on-rails rspec

我不明白为什么下面的第一个测试通过,而第二个没有。显然,这是因为我在第一个中使用了一个块,但与第二个场景相比它实际上做了什么?

require 'spec_helper'

feature "Edit user account" do
  let(:user) { FactoryGirl.create(:user) }

  before(:each) do
    sign_in_as!(user)
    visit '/settings'
  end

  scenario 'A user should be able to update their login info with current password' do
    fill_in 'user_first_name', :with => 'Mario'
    fill_in 'user_email', :with => 'mario@bross.com'
    fill_in 'user_password', :with => 'goshrooms'
    fill_in 'user_current_password', :with => 'ilovebananas'
    click_button 'Update'

    user.reload do |u|
      u.first_name.should eq 'Mario'
      u.email.should eq 'mario@bross.com'
      u.password.should eq 'goshrooms'
    end
    current_path.should eq '/settings'
    page.should have_content('You updated your account successfully.')
  end

  scenario "A user should be able to update their login info with current password" do
    fill_in "user_password", :with => "magical"
    fill_in "user_current_password", :with => 'ilovebananas'
    click_button "Update"

    current_path.should eq "/settings"
    user.reload.password.should eq "magical"
  end
end

运行测试时,我得到:

1) Edit user account A user should be able to update their login info with current password
 Failure/Error: user.reload.password.should eq "magical"

   expected: "magical"
        got: "ilovebananas"

   (compared using ==)

1 个答案:

答案 0 :(得分:2)

如上面的评论所述,密码不是数据库中的字段。因此,我没有测试密码,而是在encrypted_pa​​ssword字段上进行了测试。

 feature "* Edit user account:" do
   let(:user) { FactoryGirl.create(:user) }  

   before(:each) do
      visit "/login"
      fill_in "user_email", :with => user.email
      fill_in "user_password", :with => "ilovebananas"
      click_button "Sign in"
      visit '/settings'
      @old_encrypted_password = user.encrypted_password
    end

    scenario 'A user should be able to update their info with current password' do
      ....
      user.reload.encrypted_password.should_not eq @old_encrypted_password
    end
end