I was working on tests for blender when I came upon a need to mock or stub a back-tick operator e.g.:
`shell command`
Apparently this is just a shortcut for calling Kernel
method :`
.
So the mocking should probably be easier with Rspec mocks or mocha. something like that (didn’t test, see below):
@obj.should_receive(:`, "shell command").and_return("....")
But the problem was that I’m using RR. And RR uses a much more succinct and easy to use syntax but in this particular case it was a problem. Consider this:
# This doesn't work. it stubs the :send, not the :`
stub(@obj).send(:`, "shell command") { "...." }
RR has an instance_eval syntax, but with it I don’t know how to pass a block to the back-ticks call:
stub(@obj) do
`shell command` { "...." } # invalid syntax here ;).
end
So after some digging inside RR’s sources I got the following working:
stub(@obj).
__double_definition_create__.
call(:`, "shell command") { "...." }