My spec_helper.rb has the following configuration:
RSpec.configure do |config|
config.mock_with :mocha
# .. other configs
end
I want to test the following piece of code:
File.open('filename.zip', 'wb') do |file|
file.write('some file content')
end
So here is my spec:
file_handle = mock
file_handle.stubs(:write).with('some file content').once
File.stubs(:open).with('filename.zip', 'wb').returns(file_handle).once
But the output says there is no invocation of write
method.
Here is the output:
MiniTest::Assertion: not all expectations were satisfied
unsatisfied expectations:
- expected exactly once, not yet invoked: #<Mock:0x7fdcdde109a8>.write('some file content')
satisfied expectations:
- expected exactly once, invoked once: File.open('filename.zip', 'wb')
So am I stubbing the write
method in the right way? If not, is there any other way to write spec for method invocation inside do |obj| ..end
block?