Yes that does help.

I understand the second bullet point.  It might not be a bug if the create 
was called with a different vendor object, but it would be unless the other 
vendor object represented the same vendor in reality, (same name, same 
primary key).

I'm not sure I grok how the communication pattern is complex.

Maybe create is not the right name for the method.  It's more like write, 
or transmit.  Really, its transmit the call to the server for a create 
action.

The design of the code is for a ProspectAccount object to initialize its 
state from a given Vendor object.  Then, knowing its state, send the create 
information to the web server acting as our queuing system.


Going to lengths to avoid any_instance and its variations, I had come up 
with:

let(:vendor) { ::Vendor.new(...) }
let(:prospect) { ProspectAccount.new(vendor) }

it 'calls create instance method' do
  allow(ProspectAccount).to receive(:new).and_return(prospect)
  expect(prospect).to receive(:create).and_return(true)
  
  ProspectAccount.create_from_vendor(epoch, vendor)
end

I bulk a little at stubbing the ::new method, as it is a fundamental to the 
OOP language.


One strange point, (and maybe I'm not grokking what you are saying) is that 
it seems that your example of returning a double, is stubbing the 
object-under-test, and then you stub the :create method, which is stubbing 
the method on the object-under-test.

Further, I can see problems with stubbing the object-under-test, but not 
with writing an expectation to call a method when I am trying to verify 
that the method gets called.



class BatchController
  def create_accounts(epoch, start_time)
    vendors = Vendor.new_vendors(start_time)

    vendors.each do |vendor|
      ProspectAccount.new(vendor).create(epoch)
    end
  end
...
end

describe '#create' do

  let(:vendors) { [ ::Vendor.new(...), ::Vendor.new(...), ::Vendor.new(...) 
] }
  let(:prospect) { ProspectAccount.new(vendor) }

  it 'calls create instance method' do
    allow(Vendor).to receive(:new_vendors).and_return(vendors)
    expect_any_instance_of(ProspectAccount).to receive(:create).exactly(3).
times.and_return(true)

    BatchController.create(epoch, start_time)
  end
end


Here, it does not seem that I'm stubbing the object-under-test.

Maybe I am, since I am stubbing every instance of ProspectAccount, and I'm 
testing if those objects call the #create method.

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rspec+unsubscr...@googlegroups.com.
To post to this group, send email to rspec@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/bcb391cc-989b-499a-83b5-6fb1174220e7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to