我的 spec_helper.rb 具有以下配置:
RSpec.configure do |config|
config.mock_with :mocha
# .. other configs
end
我想测试以下代码:
File.open('filename.zip', 'wb') do |file|
file.write('some file content')
end
所以这是我的规格:
file_handle = mock
file_handle.stubs(:write).with('some file content').once
File.stubs(:open).with('filename.zip', 'wb').returns(file_handle).once
但是输出显示没有调用write
方法。
这是输出:
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')
那么我是否write
以正确的方式存根该方法?如果没有,是否有任何其他方法可以在块内编写方法调用规范do |obj| ..end
?