Reputation: 865
I would like to test if a method, in this case 'puts', gets called when I include the Foo module into a class and call 'bar'.
require 'minitest/autorun'
module Foo
def bar
puts 'bar'
end
end
class FooTest < MiniTest::Unit::TestCase
def setup
@class = Class.new do
extend Foo
end
end
def test_if_bar_method_calls_puts
mock = MiniTest::Mock.new
mock.expect(:puts, nil, ['bar'])
@class.bar
assert mock.verify
end
end
Upvotes: 4
Views: 3322
Reputation: 2405
You could do something like this:
def test_if_bar_method_calls_puts
mock = MiniTest::Mock.new
mock.expect(:puts, nil, ['bar'])
@class.stub :puts, -> (arg) { mock.puts arg } do
@class.bar
end
assert mock.verify
end
Upvotes: 3