我有一个模型,它使用attr_encrypted
gem 来加密密码。
class Credential < ApplicationRecord
validates :user_name, presence: true
enum credential_type: { windows: 1, linux: 2 }
attr_encrypted :user_pass, key: :encryption_key
def encryption_key
# Some complex logic
end
end
我正在学习编写测试用例,我的工厂如下所示:
FactoryBot.define do
factory :credential do
user_name { "rmishra" }
user_pass { "secret" }
credential_type { "linux" }
encryption_key { "abcdefghijklmnopqrstuvw123456789" }
end
end
我的规范文件如下所示:
RSpec.describe Credential, type: :model do
let(:credential) { create(:credential) }
...
end
如何encryption_key
在工厂定义中存根方法,当时正在使用create
?