Last Updated: February 25, 2016
·
2.352K
· skinnyfit

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!

1 Response
Add your response

Thanks! This was really tripping me up!

over 1 year ago ·