Rails Test & Mocha: How to silence a specific model - conditional any_instance?

I want to drown out only a specific model, but not just a specific object, but not every instance

eg. This class is 'Person' with the attributes 'name' (string) and 'cool' (boolean). We have two models:

person_bill: name: bill cool: false person_steve: name: steve cool: false 

Now I want to drown out only steve, which works well:

 p1 = people(:person_steve) p1.stubs(:cool? => true) assert p1.cool? #works 

But if I load the model from the database again, it’s not:

 p1 = people(:person_steve) p1.stubs(:cool? => true) p1 = Person.find_by_name p1.name assert p1.cool? #fails!! 

This works, but also affects Bill, which should not:

  Person.any_instance.stubs(:cool? => true) assert people(:person_bill).cool? #doesn't fails although it should 

So, how can I just kill Steve, but anyway? Is there any conditional any_instance like

  Person.any_instance { |p| p.name == 'Steve' }.stubs(:cool? => true) 

Thanks in advance!

+4
source share
1 answer

Why not just drown out the method that generates the object?

 Person.stubs( :find_by_name ). returns( stub(:cool? => true) ) 

I think the correct (and simple) answer. I'm pretty sure there is nothing like your any_instance syntax. You can find something useful in the sequence syntax:

Could you give another example of what you are looking for? Good luck

+7
source

All Articles