Rspec/FactoryBot 读取我之前的 schema.db 文件

Rspec/FactoryBot reads my previous schema.db file

在最近的更改中,我将 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

然而,当我尝试 tu 运行 my rspec 时,我重复出现错误:

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

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

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

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

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

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

迁移测试数据库是您不需要做的事情。只需使用 rails db:test:prepare.

设置架构