我正在尝试在不加载整个 Rails 应用程序的情况下测试模型。我只想包含与我正在测试的课程相关的导轨部分。如何正确要求 active_model/validations?我以为我做对了,但是下面的设置抛出了这个错误:
undefined method `validate' for Project::Media:Class (NoMethodError)
正在测试的模型:
#app/models/project/media.rb
class Project::Media < Project #Project inherits from ActiveRecord::Base
validate :allowable_media_source
before_save : classify_link
#next, all the methods
end
规格:
#spec/models/project/media_spec.rb
class ActiveRecord
class Base; end
end
class Project; end
require_relative '../../../app/models/project/media.rb'
require 'active_model/validations'
describe Project::Media do
#then tests
end