One thing I've never understood about rspec, and specifically the warning 
against using allow any instance of, is how to test code that creates objects 
that you will want to stub.

Since the objects are not created in rspec (but instead inside the tested 
code), I can't stub them, so I often have to either use allow_any_instance_of 
or stub out the new method on the class to return doubles.

I understand why allow_any_instance_of is code-smelly but I've never really 
seen a good approach to this. 





> On Sep 25, 2015, at 10:23 AM, Myron Marston <[email protected] 
> <mailto:[email protected]>> wrote:
> 
> The relish docs are kept under source in the repository for each gem under 
> the features directory. The ones you mentioned are here:
> 
> https://github.com/rspec/rspec-mocks/blob/master/features/old_syntax/any_instance.feature
>  
> <https://github.com/rspec/rspec-mocks/blob/master/features/old_syntax/any_instance.feature>
> https://github.com/rspec/rspec-mocks/blob/master/features/working_with_legacy_code/any_instance.feature
>  
> <https://github.com/rspec/rspec-mocks/blob/master/features/working_with_legacy_code/any_instance.feature>
> Please submit suggested doc changes as a PR to the repo.
> 
> Thanks,
> Myron
> 
> 
> On Fri, Sep 25, 2015 at 2:18 AM, James Doyley <[email protected] 
> <mailto:[email protected]>> wrote:
> Hi,
> 
> I was looking at the docs, specifically
> https://relishapp.com/rspec/rspec-mocks/docs/old-syntax/any-instance 
> <https://relishapp.com/rspec/rspec-mocks/docs/old-syntax/any-instance>
> https://relishapp.com/rspec/rspec-mocks/docs/working-with-legacy-code/any-instance
>  
> <https://relishapp.com/rspec/rspec-mocks/docs/working-with-legacy-code/any-instance>
> 
> I agree that it's a design smell, but I want to suggest an example.  I 
> couldn't find out how to contribute to the documentation hence posting here.
> 
> I was just thinking a better example as an alternative would be
> 
> RSpec.describe User, type: :model do
>   subject(:user){ User.new }
>   before(:each) do
>     allow(user).to receive(:foo).and_return("bar")
>   end
> 
> 
> 
> -- 
> 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] 
> <mailto:[email protected]>.
> To post to this group, send email to [email protected] 
> <mailto:[email protected]>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rspec/a725b287-f0ac-4315-83ca-c4ab9c03ba05%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/rspec/a725b287-f0ac-4315-83ca-c4ab9c03ba05%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.
> 
> 
> -- 
> 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] 
> <mailto:[email protected]>.
> To post to this group, send email to [email protected] 
> <mailto:[email protected]>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rspec/CADUxQmspk2-5i197tVPaR27qSq-0daK7EWd-im32qy3UT0CHog%40mail.gmail.com
>  
> <https://groups.google.com/d/msgid/rspec/CADUxQmspk2-5i197tVPaR27qSq-0daK7EWd-im32qy3UT0CHog%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

----

Jason Fleetwood-Boldt
[email protected] <mailto:[email protected]>
http://www.jasonfleetwoodboldt.com/writing 
<http://www.jasonfleetwoodboldt.com/writing>



-- 
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/A0C13FEA-7B3A-43EB-86DF-B424622A7155%40datatravels.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to