How toStub a method in a class with Minitest
If you have a method in a Ruby class that you want to stub for testing, you can use Minitest stub
method like:
MyClass.stub(:method, "value") do
# Here the method is stubbed
end
But if the method is a private method, that will not work, in that case you have to change it with a more longer version:
MyClass
.any_instance
.stubs(:method)
.returns("value") do
# Here the method is stubbed
end
This works because Minitest will now stub any instance of the class and since it works at the instance level it will be able to stub private methods too.