unit testing - Mock objects for Grepping a binary file in Ruby -
for scenario, grep binary file manufacturer string in ruby. create associated mock object in rspec. tried following did not help:
file.stub!(:exist?).with(<binary_file>).and_return(true) file.stub!(:read?).with(<binary_file>).and_return('xyz')
what missing here?
firstly, remove !
#stub
, not used in newer versions of rspec. secondly, stub(:read?)
should stub(:read)
, since not predicate method.
without seeing implementation code, changes should put in right direction.
that said, avoidable, tend disagree stubbing class methods this, since there pretty big risk of state leakage through tests. instead design component searching in file accept instance of io
, becomes easier pass in mock object entirely disposable.
Comments
Post a Comment