Created
August 27, 2016 13:50
-
-
Save esebastian/a6734d29b9ceb5d942fe986b8d610ca2 to your computer and use it in GitHub Desktop.
Minitest class method mock sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/autorun' | |
class VoiceServiceTest < Minitest::Test | |
def test_calls_remote_service | |
remote_service_mock = Minitest::Mock.new | |
remote_service_mock.expect(:call, { result: 'ochenta y ocho'}.to_json, ['88.wav']) | |
result = nil | |
RemoteService.stub(:request_recognize, remote_service_mock) do | |
result = VoiceService.recognize('88.wav') | |
end | |
assert_equal 'ochenta y ocho', result | |
remote_service_mock.verify | |
end | |
end | |
require 'json' | |
class VoiceService | |
def self.recognize(filename) | |
response = RemoteService.request_recognize(filename) | |
JSON.parse(response)['result'] | |
end | |
end | |
class RemoteService | |
def self.request_recognize(resource) | |
{result: :whatever}.to_json | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment