我正在尝试通过将模块包含在基本的mock_model对象中来规范模块。但是,当我调用模块中定义的实例方法时,ActiveRecord尝试与数据库建立连接。
模块:
module Stuff
module SoftDelete
extend ActiveSupport::Concern
def soft_delete
puts "Called here"
end
end
end
规范:
describe Stuff::SoftDelete do
class Network < ActiveRecord::Base
include Stuff::SoftDelete
attr_accessor :deleted_at
end
before (:each) do
@network = mock_model(Network)
end
context "When a record is deleted" do
it "is marked as deleted" do
@network.soft_delete
end
end
end
运行此规范时,会发生以下错误:
1) Stuff::SoftDelete When a record is deleted is marked as deleted
Failure/Error: @network.soft_delete
ActiveRecord::ConnectionNotEstablished:
ActiveRecord::ConnectionNotEstablished
# ./spec/apoc/soft_delete_spec.rb:18:in `block (3 levels) in <top (required)>'
注意:如果我将SoftDelete模块包含在真正的ActiveRecord类中,它将起作用。似乎mock_model无法处理模块。
在这一方面会有所帮助。 谢谢!
答案 0 :(得分:1)
你相信ActiveRecord吗?如果是这样,不要继承它;单独测试您的模块。如果您的模块包含调用ActiveRecord方法,则将它们存根并仅测试您的代码。