I need to drown out all instances of the model that have a specific attribute or set of attributes. For example, using ActiveRecord:
let(:model1) { Model.create!(uid: 1) }
let(:model2) { Model.create!(uid: 2) }
before do
allow(model1).to receive(:foo).and_return :bar
allow(model2).to receive(:foo).and_return :baz
end
it do
expect(model1.foo).to eq :bar
expect(model2.foo).to eq :baz
new_instance_of_model1 = Model.find_by(uid: 1)
new_instance_of_model2 = Model.find_by(uid: 2)
expect(new_instance_of_model1.foo).to eq :bar
expect(new_instance_of_model2.foo).to eq :baz
end
Is there a way to drown out all instances Modelthat have one uid: 1?
I am looking for something like:
allow_any_instance_of(Model).with_attributes(uid: 1).to receive(:foo).and_return(:bar)
allow_any_instance_of(Model).with_attributes(uid: 2).to receive(:foo).and_return(:baz)
Note:
I can not use something like:
allow(Model).to receive(:find).with(1)and_return(model1)
allow(Model).to receive(:find).with(2)and_return(model2)
because there are many other ways to get to the model (associations, areas, Arel, etc.)
source
share