Rspec 不会重新加载更改的密码属性 - 为什么? [英] Rspec doesn't reload changed password attribute - why?

查看:42
本文介绍了Rspec 不会重新加载更改的密码属性 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am running Rails 3.2.1 and rails-rspec 2.8.1....When I run the test for resetting a user's password, it seems that reloading the user object doesn't load the new password attribute....

这是我的测试代码:

 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 } 通行证

所以这告诉我密码被正确保存,但没有被重新加载......任何想法为什么?我正在使用 Rails 3.2.xhas_secure_password"方法来滚动登录/密码功能.这意味着 password_digest 是保存到数据库的内容,而 password 是一个虚拟属性(我认为).

So this tells me the password is being saved correctly, but not being reloaded...any ideas why? I am using the Rails 3.2.x "has_secure_password" method for rolling the login/password features. This means a password_digest is what gets saved to the database, and password is a virtual attribute (I think).

推荐答案

刚刚理解:let 块在你召唤它们之前不会被加载.

Just understood: let blocks are not loaded till you summon them.

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

So, because you didn't use old_password anywhere before:

teacher.reload.password.should_not == old_password

相当于:

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

这就是它不能失败的原因!

That's why it just can't fail!

如果您希望它正确失败:

If you want it to fail correctly:

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

这篇关于Rspec 不会重新加载更改的密码属性 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆