3

Using MiniTest::Spec and Mocha:

 describe "#devices" do
   it "scopes the devices by the provided :ip_list" do
     ips = 'fast tests ftw!'
     ds = DeviceSearch.new ip_list: ips
     Device.expects(:scope_by_ip_list).once.with(ips)
     ds.devices
   end
 end

When I make the code work correctly, this test will fail, because calling Device.expects(:scope_by_ip_list) also stubs Device.scope_by_ip_list, and since I don't specify a .returns(Devices.scoped) or some such, it stubs out the method with nil. So, in my code which properly scopes a list of devices and then does further operations, the further operations blow up.

I don't want to have to specify a .returns parameter, though, because I totally don't care what it returns. I don't want to stub the method at all! I just want to set up an expectation on it, and leave it functioning just the way it is.

Is there a way to do that?

(To me, it seems very awkward to say something like Device.expects(:foo).returns('bar')—when I say that Model expects method, I'm not saying to stub that method! We can say Device.stubs(:foo), if we want to stub it.)

chadoh
  • 4,343
  • 6
  • 39
  • 64
  • Does this answer your question? [Ruby Mocha: is there an equivalent to rspec-mocks' #and\_call\_original?](https://stackoverflow.com/questions/21715174/ruby-mocha-is-there-an-equivalent-to-rspec-mocks-and-call-original) – josemigallas Jun 21 '22 at 10:05

1 Answers1

0

The behavior is intended and can't be changed. Look at the following post to see how it can be circumwented:

rspec 2: detect call to method but still have it perform its function

Community
  • 1
  • 1
moritz
  • 25,477
  • 3
  • 41
  • 36
  • Thanks for the interesting link! However, I am using Minitest and doing the stubbing/expectations with Mocha, so the RSpec example is largely irrelevant. – chadoh May 08 '12 at 17:44
  • Unfortunately, I overlooked that. Sorry for that. – moritz May 09 '12 at 08:31