0

最初,我开始使用 构建身份验证bcrypt-ruby,并且有一个有效的 Rspec 用户模型来测试是否可以使用有效密码在数据库中找到该用户。从那时起,我决定改用 Devise,但不幸的是,现在我的规范失败了,因为我has_secure_password不再在我的用户模型中使用,所以该authenticate方法是未定义的。从我通过搜索设计 wiki 发现的内容来看,这似乎更适合通过使用设计帮助器的控制器规范,但我很困惑,因为我只想像以前一样检查模型以确保用户可以进行身份验证并且不完全确定要替换authenticate的内容。

当我使用 bcrypt 时,我的user_spec.rb文件之前通过has_secure_password

require 'spec_helper'

describe User do

  it { should respond_to(:authenticate) }

  describe "return value of authenticate method" do
    user = FactoryGirl.create(:user)
    before { user.save }
    let(:found_user) { User.find_by(email: user.email) }

    describe "with valid password" do
      it { found_user.authenticate(user.password).should eq user }
    end

    describe "with invalid password" do
      let(:user_for_invalid_password) { found_user.authenticate("invalid") }

      it { user_for_invalid_password.should be_false }
    end
  end

end
4

1 回答 1

1

查看devise rspec 文档- 您需要设置一些帮助程序,以便对用户进行身份验证并以登录用户身份执行任务。

我不会太担心测试实际的身份验证逻辑——这应该包含在设计 gem 本身的规范测试中。

于 2013-11-11T00:53:06.440 回答