繁体   English   中英

在 Rails 6 / FactoryBot 6 中,如何模拟 attr_encrypted 属性?

[英]In Rails 6 / FactoryBot 6, how do I simulate attr_encrypted attributes?

最近升级到 Rails 6 和 FactoryBot 6.2.0。 我有这个 model

class Store < ApplicationRecord

    …
  attr_encrypted :ein_number,
    key: APP_CONFIG[:app_encryption][::Rails.env][‘secret_key’]

我有对应的工厂

FactoryBot.define do
  factory :store do
    name                          { "Test Store” }
    …
    ein_number                 { "00-0000000" }

但是现在当我 go 并创建这个工厂的实例时,我得到了错误

  Failure/Error: @store = create :store
  
  NoMethodError:
    undefined method `encrypted_ein_number_iv' for #<Store:0x00007feec319fbe0>

不确定 Rails 或 FactoryBot 发生了什么变化,但在我没有定义这样的方法的情况下,这之前有效。 无论如何,这似乎是一种黑客行为。 有没有更优雅的方式在工厂中定义这个属性?

奇怪的是,将我的 model 上的 attr_encryptor 更新为

  attr_encrypted :ein_number,
    key: APP_CONFIG[:cfs_encryption][::Rails.env]['secret_key'],
    algorithm: 'aes-256-cbc', mode: :single_iv_and_salt, insecure_mode: true

解决了问题(工厂正常创建了 object 的实例)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM