to an API call during the test,I want to stub the OpenURI-open-method to return a file which content I packed up in a constant. However, in the same parsing method, all other calls to OpenURI open should be handled normally
@obj.should_receive(:open).with do |arg|
if arg == mypath # mypath is a string constant like "http://stackoverflow.com/questions"
obj=double("object_returned_by_openuri_open") # create a double
obj.stub(:read).and_return(TESTFILE) # with a stub
obj #return the double
else
open(arg) # call original Open URI method in all other cases
end
end
However, when calling the parsing method, this does not work and it returns "NoMethodError:
undefined method read for nil:NilClass"
in the line f = open(mypath).read
of my parsing method.
Does anybody know how can achieve this sort of "partial method stubbing" (stubbing a method for one specific argument, call original for others). The other files are Images, so I don't want to store them as constants in my source code. In an extension to make the tests network-independent, I could also return an local image file in the else
-case.
I would be glad about any suggestions and hints :)