2

我正在运行 Rails 3.2.1 和 rails-rspec 2.8.1 ....当我运行重置用户密码的测试时,似乎重新加载用户对象不会加载新的密码属性....

这是我的测试代码:

 describe "Reset password page" do
    let(:teacher) { FactoryGirl.create(:teacher, :email=>"jane@gmail.com")}
    let(:old_password) { teacher.password }
    before do
      visit reset_password_form_path
      fill_in "Email", with: teacher.email
      click_button "Reset Password"
    end
    it { should have_content('Your new password has been emailed to the address you provided.')}
    specify { Teacher.find_by_email("jane@gmail.com").password.should_not == old_password }
    ## specify { teacher.reload.password.should_not == old_password }  ##THIS FAILS
  end

指定 {teacher.reload.password.should_not == old_password }失败但指定 { Teacher.find_by_email("jane@gmail.com").password.should_not == old_password } PASSES

所以这告诉我密码被正确保存,但没有被重新加载......有什么想法吗?我正在使用 Rails 3.2.x “has_secure_password” 方法来滚动登录/密码功能。这意味着密码摘要是保存到数据库的内容,密码是虚拟属性(我认为)。

4

1 回答 1

1

刚刚明白:let在你召唤它们之前不会加载块。

所以,因为你之前没有old_password在任何地方使用过:

teacher.reload.password.should_not == old_password

相当于:

teacher.reload.password.should_not == teacher.reload.password

这就是为什么它不能失败!

如果您希望它正确失败

old_password #because it's triggered, the value is set and won't be set again
teacher.reload.password.should_not == old_password

编辑:

describe "Reset password page" do
  let(:teacher) { FactoryGirl.create(:teacher, :email=>"jane@gmail.com")}

  before do
    @old_password = teacher.password
    visit reset_password_form_path
    fill_in "Email", with: teacher.email
    click_button "Reset Password"
  end

  specify { teacher.reload.password.should_not == @old_password }
end
于 2012-05-16T21:52:59.787 回答