我正在处理一个旧项目(最近从Rails 4.1升级到5.2),我不得不更改一个关联表。之前:
报告有很多客户,客户有很多报告。现在,我创建了一个ClientsReport
表,该表不仅包含client_id和report_id,而且还具有一个id
作为primary_key
和一个can_manage
(布尔值)。
使用rspec测试时,调用reports_clients.for(report).first.can_manage
时出现错误
如果unKnownAttribute can_manage
或<ClientsReport client_id: 1, report_id:3>
都不用id
表示can_manage
没符号
所以看起来它正在使用旧模式。
也尝试按照建议的here添加ActiveRecord::Migration.maintain_test_schema!
,但我不确定
我尝试运行rake:db:prepare
,但它给我带来了很多错误,现在看来我打破了测试数据库,因为我有4个失败的测试,现在有166 ...
我的spec_helper.rb看起来像这样:
Spork.prefork do
require 'simplecov'
SimpleCov.start 'rails'
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'devise'
require './spec/controllers/controller_helpers.rb'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
config.include ControllerHelpers, type: :controller
config.include Capybara::DSL
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
config.infer_spec_type_from_file_location!
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.include FactoryGirl::Syntax::Methods
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
答案 0 :(得分:1)
You can completely reset the test DB with this command, after making sure your schema is up to date.
bin/rails db:environment:set db:drop db:create db:schema:load RAILS_ENV=test