Rspec stub any_instance with attributes

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 # => passes
  expect(model2.foo).to eq :baz # => passes

  ################################################
  #### Here is the issue I'm trying to solve: ####
  ################################################
  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 # => fails
  expect(new_instance_of_model2.foo).to eq :baz # => fails
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.)

+4
source share
1 answer

This is not a good idea, but you can do this by passing a block argument receiveinstead of a chain of methods, for example:

allow_any_instance_of(Model).to receive(:foo) do |model|
  case model.uid
  when 1
    :bar
  when 2
    :baz
  end
end
0
source

Source: https://habr.com/ru/post/1673665/


All Articles