Reputation: 355
So, I have the following lines of code that I want to mock test with RSpec.
class BaseService
MAX_BODY_SIZE = 100000.freeze
def translate(body, language, account)
return body if body.blank?
translate_body_in_chunks(body, language, account) if body.size > MAX_BODY_SIZE
end
def translate_body_in_chunks(body, language, account)
# some API call that I don't want to call while testing
end
end
I want to test if translate_body_in_chunks
is being called or not.
body = 'a' * 10000000
mock = double(described_class)
allow(mock).to receive(:translate).with(body, 'en', '')
expect(mock).to receive(:translate_body_in_chunks)
I know this test won't work. I just added it here to give you a hint on what I want to test.
Upvotes: 0
Views: 150
Reputation: 114138
Something like this should work:
describe BaseService do
describe '#translate' do
context 'with a body exceeding MAX_BODY_SIZE' do
let(:body) { 'a' * (BaseService::MAX_BODY_SIZE + 1) }
it 'calls translate_body_in_chunks' do
expect(subject).to receive(:translate_body_in_chunks)
subject.translate(body, 'en', '')
end
end
end
end
subject
refers to an instance created via BaseService.new
.
Note that in general, you shouldn't test your method's implementation details. Instead, try to test the method's behavior.
Upvotes: 1