I’m trying to spec a module by including it in a basic mock_model object. However, when I call the instance method defined in the module ActiveRecord tries to establish a connection with the database.
The module:
module Stuff
module SoftDelete
extend ActiveSupport::Concern
def soft_delete
puts "Called here"
end
end
end
The Spec:
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
When I run this Spec, the following error occurs:
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)>'
Note: If I include the SoftDelete module in a real ActiveRecord class, it will work. It just seems that mock_model isn’t able to deal with the module.
Would love some help on this one.
Thanks!
Do you trust ActiveRecord? If so, don’t inherit from it; test your module in isolation. If your module includes calls ActiveRecord methods, stub them and test only your code.