【问题标题】:Rspec/FactoryBot reads my previous schema.db fileRspec/FactoryBot 读取我之前的 schema.db 文件
【发布时间】:2020-11-20 14:28:37
【问题描述】:

在最近的更改中,我将列 first_name 和 last_name 合并为一个 full_name。我进行了迁移,并且在我的 schema.rb 上一切都是最新的。我的用户工厂现在看起来像这样:

FactoryBot.define do
  password = Faker::Internet.password

  factory :user do
    email { Faker::Internet.email }
    full_name { Faker::Name.name }
    password { password }
    password_confirmation { password }
    owner { 1 }
    registration_step { :finished }
    confirmed_at { Time.current }
  end
end

但是,当我尝试运行我的 rspec 时,我得到重复的错误:

  Failure/Error: let(:user) { create :user }
  
  NoMethodError:
    undefined method `full_name=' for #<User:0x00007ff5e597e310>

此外,运行 RAILS_ENV=test rake db:migrate 会以某种方式将我的 schema.db 重写为之前的状态,其中 first_name 和 last_name 仍然存在而不是 full_name。这些列不再存在于我的迁移文件中!

FactoryBot/rspec 如何仍然能够加载我的旧版本架构以及如何解决此问题?

注意我使用了 rails db:drop db:create db:migrate 但它没有修复它。

【问题讨论】:

    标签: ruby-on-rails rspec factory-bot ruby-on-rails-6


    【解决方案1】:

    使用 rails db:test:prepare 将测试数据库架构设置为等于您的 schema.rb 文件。

    您可能在开发时回滚了迁移并在更改后重新运行它,但在测试时它使用旧代码运行并且由于它已经运行一次而不再运行。

    迁移测试数据库是您不需要做的事情。只需使用rails db:test:prepare 设置架构即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-02
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-25
      • 1970-01-01
      相关资源
      最近更新 更多