Last active
August 29, 2015 14:10
-
-
Save jbr/2c54ff6a5a6eb11a3719 to your computer and use it in GitHub Desktop.
Why doesn't mocha stub Tempfile#unlink?
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 'mocha/api' | |
t = Tempfile.new %w[ temp .file ] | |
## show that another method on a tempfile can successfully be stubbed | |
t.stubs(:closed?).returns :something_it_would_not_normally_return | |
t.closed? #=> :something_it_would_not_normally_return | |
## show that an imaginary method on a tempfile can successfully be stubbed | |
t.stubs(:wiggly?).returns :yes_of_course | |
t.wiggly? #=> :yes_of_course | |
## show that Tempfile#unlink specifically cannot be stubbed | |
t.path #=> not nil, as is reasonable, it hasn't been unlinked | |
t.stubs(:unlink).returns :another_return_value | |
t.unlink #=> t | |
t.path #=> nil, because the actual unlink was called | |
## show that unlink can be stubbed on other objects | |
o = Object.new | |
o.stubs(:unlink).returns :i_am_not_linked | |
o.unlink #=> :i_am_not_linked |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment