Reputation: 844
I'm writing a test method for Class A, method called m().
m() calls f() on Class B through an instance of B called 'b', but I mock that method call using-
def test_m
@a = A.new
b_mock = MiniTest::Mock.new
b_mock.expect(:f, 'expected_output')
def b_mock.f()
return 'expected output'
end
@a.b = b_mock
end
Now A has another method m1(), how can a mock a call to it and get a constant output, using the above or a better approach with Minitest?
Error-
NoMethodError: unmocked method :get_group_by_name, expected one of [:]
Upvotes: 1
Views: 4595
Reputation: 6132
You can use MiniTest Object#stub method, it redefines the method result for the duration of the block.
require 'minitest/mock'
class A
def m1
m2
'the original result'
end
def m2
'm2 result'
end
end
@a = A.new
@a.stub :m1, "the stubbed result" do
puts @a.m1 # will print 'the stubbed result'
puts @a.m2
end
Read more: http://www.rubydoc.info/gems/minitest/4.2.0/Object:stub
Upvotes: 3