Stubbing gets undone?

I’m looking at an example where a stub seems to work sometimes, and
sometimes appears to become “unstubbed”. I haven’t boiled it down to a
minimal example, but it goes something like this:
---- the model:
class Premise << ActiveRecord::Base
def lookup_stuff_on_the_web
$stderr.puts(“entering lookup_stuff_on_the_web with #{self}”)

end
end
---- the rspec test file:
describe Analysis do
before(:each) do
@premise = Factory(:premise)
@premise.stub(:lookup_things_on_the_web)
$stderr.puts(“just stubbed #{@premise.inspect}”)
end

it “should not hit the web” do
@premise.lookup_things_on_the_web.should_not raise_exception
end
end

When I run the test, I see:

just stubbed #<Premise id: 1, user_id: 1, …>
entering lookup_stuff_on_the_web with #<Premise id: 1, user_id: 1, …>
Failure/Error: …
WebMock::NetConnectNotAllowedError:

As far as I can see, the Premise that’s being stubbed and the Premise
whose lookup_stuff_on_the_web method is getting called are one and the
same. What I can’t figure out is why the stub isn’t being honored.
(FWIW, the same setup code is working in other tests.)

Are there any known gotchas I should be looking for? Or specific things
I should try to uncover the cause?

TIA.

  • ff

Hmm - when I print puts(premise) rather than puts(premise.inspect), I
can see that they actually have different addresses, so they’re not
really equal:

just stubbed #Premise:0x000001069b99f0
entering lookup_stuff_on_the_web with #Premise:0x00000106b08ec8

Now my problem is figuring out why/where the other Premise is getting
created.

In other words, move along…nothing to see here. :slight_smile:

  • ff

Is #unstub being called at any point?

Scott

Scott T. wrote in post #981963:

Is #unstub being called at any point?
Scott

No, but I think I see what’s happening: I’m stubbing a particular
(in-memory) instance of an AR. Later on in my code, the same AR is
fetched, but now has a different in-memory address, and thus isn’t
subject to stubbing. (Is my analysis plausible?)

So my new question is: can I stub Premise#lookup_stuff_on_the_web for
all objects? (I could just set up WebMock to return a specific
response, but it’s a complex data structure and it would be much cleaner
to stub Premise#lookup_stuff_on_the_web instead.)

  • ff

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs