ruby-on-rails - Rspec 加载旧模式
问题描述
我正在处理一个遗留项目(最近从 rails 4.1 升级到 5.2),我不得不更改关联表。之前:报告有很多客户,客户有很多报告。现在我创建了一个ClientsReport
表,不仅包含 client_id 和 reports_id,而且还有一个id
asprimary_key
和一个can_manage
(布尔值)。
使用 rspec 进行测试,当我调用没有reports_clients.for(report).first.can_manage
符号 if或unKnownAttribute can_manage
<ClientsReport client_id: 1, report_id:3>
id
can_manage
所以看起来它正在使用旧模式。
也尝试按照这里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
解决方案
在确保您的架构是最新的之后,您可以使用此命令完全重置测试数据库。
bin/rails db:environment:set db:drop db:create db:schema:load RAILS_ENV=test
推荐阅读
- php - 如何在运行脚本时禁用 opcache.enable_cli?
- python - 如何遍历列表列表并在每个列表中选择随机元素?
- node.js - 将猫鼬查找与动态查询一起使用
- algorithm - 根据所需的平均颜色编辑图像
- visual-studio-2017 - VS2019 包与项目参考(最小版本)
- node.js - 有没有办法在护照js中为不同路由的不同用户实现不同的身份验证策略?
- c# - Scenario 和 Scenario outline 有什么区别?
- typescript - Typescript GRPC 客户端拦截器
- typescript - TypeScript 在对未知变量进行类型检查后仍会触发错误 ts2571
- flutter - 在调试和配置文件模式下工作正常,在发布模式下不工作