Hello!

We consider `allow_any_instance_of(…)` a feature for working with legacy code 
and not to be the first choice, but it’s currently still supported, you can 
find the documentation here:

https://relishapp.com/rspec/rspec-mocks/v/3-9/docs/working-with-legacy-code/any-instance

To replace it we recommend you be specific about your instances.

e.g for your example:

```
config.before(:each) do
allow(User).to receive(:find) do
user = instance_double(User)
allow(user).to receive(:send_to_stripe).and_return(true)
user
end
end

```

But really, I wouldn’t do that at all, because why does every test need this? 
I’d more likely recommend stubbing the right call at the right time, with the 
right calls. Otherwise my code could call send_to_stripe at any point and my 
tests would never notice...

Cheers
Jon
----------------
[email protected]
https://jonrowe.co.uk

On 25 September 2020 at 16:31, Jack Royal-Gordon wrote:
> I’ve just upgraded from Rspec 2.99 to 3.9, and have discovered that 
> “allow_any_instance_of” is no longer supported. I’m not sure how best to go 
> about replacing it, as it is in use in most of my tests. I have a User class, 
> and most tests involve a user. Behind the scenes, many of the user methods 
> call the Stripe API to adjust subscriptions, view and preview invoices, etc. 
> Currently, I’ve got the following in “spec_helper.rb”:
>
> > config.before(:each) do
> > allow_any_instance_of(User).to receive(:send_to_stripe).and_return(true)
> >
> > end
> >
>
>
> In most cases the calls to the Stripe API go through that method (I used 
> instance stubs as necessary in tests that use other facets of the Stripe 
> API). I understand that the preferred approach is to stub an instance rather 
> than a whole class. But to do this, everywhere I create a user I would have 
> to add “allow(xxx).to receive(:send_to_stripe).and_return(true). Most (if not 
> all) of my users are created with calls to FactoryBot.In 
> (http://FactoryBot.In) the interest of DRYing my tests, is there a way I can 
> leverage that to set a stub on the instance being created by FactoryBot?

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/dejalu-217-5242bc7e-977b-41a6-b172-a7e4525c8701%40jonrowe.co.uk.

Reply via email to