1

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

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 ==)
4

1 回答 1

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
于 2012-12-28T22:15:57.250 回答