RSpec stub for certain arguments only
Just came across an RSpec controller test that stubbed File.stat
like so:
File.stub(:stat).with('some/shizzle.txt').and_return(mock_file)
This was a problem, because elsewhere in the same controller action I needed to use unstubbed File.stat
with other files. The solution is pretty simple:
File.stub(:stat).and_call_original
File.stub(:stat).with('some/shizzle.txt').and_return(mock_file)
Now any calls to File.stat
will behave normally, unless the filename is some/file.txt
, in which case you'll get the mock!
Written by Richard Worrall
Related protips
1 Response
Thanks! This was really tripping me up!
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#