简体   繁体   中英

RSpec: Mock not working for instance method and multiple calls

I'm trying to mock PriceInspector#get_latest_price below to test OderForm . There are two orders passed in, hence, I need to return two different values when mocking PriceInspector#get_latest_price . It all works fine with the Supplier model (ActiveRecord) but I can't run a mock on the PriceInspector class:

# inside the test / example
expect(Supplier).to receive(:find).and_return(supplier_1) # first call, works
expect(PriceInspector).to receive(:get_latest_price).and_return(price_item_1_supplier_1) # returns nil
expect(Supplier).to receive(:find).and_return(supplier_2) # second call, works
expect(PriceInspector).to receive(:get_latest_price).and_return(price_item_2_supplier_1) # returns nil

class OrderForm
  include ActiveModel::Model

  def initialize(purchaser)
    @purchaser = purchaser
  end  

  def submit(orders)        
    orders.each do |supplier_id, order_items|      
      @supplier = Organization.find(supplier_id.to_i)
      @order_item = OrderItem.save(                    
        price_unit_price: PriceInspector.new(@purchaser).get_latest_price.price_unit_price
      )
      [...]
    end
  end
end

class PriceInspector
  def initialize(purchaser)
    @purchaser = purchaser    
  end

  def get_latest_price
    [...]
  end
end

Edit

Here's the updated test code based on Bogieman's answer:

before(:each) do        
  expect(Organization).to receive(:find).and_return(supplier_1, supplier_2)

  price_inspector = PriceInspector.new(purchaser, item_1)
  PriceInspector.stub(:new).and_return price_inspector
  expect(price_inspector).to receive(:get_latest_price).and_return(price_item_1_supplier_1)  
  expect(price_inspector).to receive(:get_latest_price).and_return(price_item_2_supplier_2)                
end

it "saves correct price_unit_price for first OrderItem", :focus do        
  order_form.submit(params)
  expect(OrderItem.first.price_unit_price).to be_within(0.01).of(price_item_1_supplier_1.price_unit_price)  
end

I think this should fix the instance method problem and allow you to check for the two different returns (provided you pass in the purchaser or a double) :

price_inspector = PriceInspector.new(purchaser)
PriceInspector.stub(:new).and_return price_inspector

expect(price_inspector).to receive(:get_latest_price).and_return(price_item_1_supplier_1)
expect(price_inspector).to receive(:get_latest_price).and_return(price_item_2_supplier_1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM