stevenspiel
stevenspiel

Reputation: 5999

rspec stub any_instance with attributes

I need to stub all instances of a model that have a particular 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 some way to stub all instances of Model that have uid: 1?

I'm 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't 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, scopes, Arel, etc.)

Upvotes: 4

Views: 2451

Answers (1)

brainbag
brainbag

Reputation: 1057

This isn't a very good idea, but you can accomplish this by passing a block argument to receive instead of a method chain, like this:

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

Upvotes: 3

Related Questions