I am using minitest / mock and would like to make fun of the class. I am not trying to test the model class itself, but trying to verify that the service (SomeService) interacts with the model (SomeModel).
I came up with this (Hack :: ClassDelegate), but I'm not sure if this is a good idea:
require 'minitest/autorun' require 'minitest/mock' module Hack class ClassDelegate def self.set_delegate(delegate); @@delegate = delegate; end def self.method_missing(sym, *args, &block) @@delegate.method_missing(sym, *args, &block) end end end class TestClassDelegation < MiniTest::Unit::TestCase class SomeModel < Hack::ClassDelegate ; end class SomeService def delete(id) SomeModel.delete(id) end end def test_delegation id = '123456789' mock = MiniTest::Mock.new mock.expect(:delete, nil, [id]) SomeModel.set_delegate(mock) service = SomeService.new service.delete(id) assert mock.verify end end
Iβm sure that mocking a class is not a great idea anyway, but I have a legacy system in which I need to write some tests, and I donβt want to change the system until I finish some tests around it.
source share