我已经重构了我的测试套件的一部分,以便在执行测试时不加载轨道。下面的代码是一个测试文件的示例,它只加载 Rails 的选择片段。它还伪造了“项目”类。我的问题是这个伪造的项目类最终覆盖了正常的项目类,并且涉及该项目类的所有其他测试现在都失败了。
此测试文件运行后如何取消覆盖我的项目类?
require 'active_model'
require 'active_model/validations'
require 'active_record/callbacks'
require 'active_support/core_ext/string'
class Project
include ActiveModel::Validations
include ActiveRecord::Callbacks
def initialize(attributes = {})
@general_media = attributes[:general_media]
end
attr_accessor :general_media
end
require_relative '../../../app/models/project/media.rb'
更新:我认为这接近我需要的,除了我收到关于 Project 是未初始化常量的错误。我一定是错误地实例化了这个测试类。
require 'active_model'
require 'active_model/validations'
require 'active_record/callbacks'
require 'active_support/core_ext/string' #used for '.blank?' method
require_relative '../../../app/models/project/media.rb'
describe Project::Media do
before(:all) do
class Project
include ActiveModel::Validations
include ActiveRecord::Callbacks
def initialize(attributes = {})
@general_media = attributes[:general_media]
end
attr_accessor :general_media
end
end
after(:all) { Object.send(:remove_const, :Project) }
#then all the tests