0
votes

A rspec newbie here. I understand that I can stub out a method of an object under test using 'allow( ).to receive( ).and_return' but if the object I am testing itself uses another object which has an expensive method how do I stub out the expensiv part e inside that method but still test the rest of the method?

That is if I have a method like

class Foo
  def process
   data = DataModel.fetch_data #<-- the expensive process.
    if verify(data)
      [do things]
    end
  end

  private 
  def verify(data)
    [...]
  end
end

So how would a I write an rspec test where I can provide the process method with test data so I can check out if verify and "do things" are working correctly? I don't want to execute DataModel.fetch because it is time consuming and expensive. (Rails 5.2)

1

1 Answers

1
votes

You can do:

fetched_data = instance_double("TheClassReturnedFrom#fetch_data")
allow(DataModel).to receive(:fetch_data).and_return(fetched_data)