On Fri, Sep 5, 2008 at 4:44 PM, Nick Hoffman <[EMAIL PROTECTED]> wrote:
> So, after reading all of those fantastic emails discussing testing behaviour
> vs state, I decided to try mocking and stubbing a couple of methods. I think
> I did well on my first one, but I'm not sure what the best way to spec the
> following method is:
>
>
> 1     class RentalMap
> ...
> 166   def add_properties
> 167     Property.find(:all, :conditions => 'latitude NOT NULL AND longitude
> NOT NULL').each do |p|
> 168       add_marker p.address, p.latitude, p.longitude,
> generate_marker_contents(p)
> 169     end
> 170   end
>
> In my spec, I figured I would:
> -Create a RentalMap object.
> -Mock two properties.
> -Setup expectations on the two mock properties.
> -Stub the call to Property#find .
> -Stub the call to RentalMap#add_marker .
> -Stub the call to RentalMap#generate_marker_contents .
>
> Here's what I wrote:
>
> 185   describe '#add_properties' do
> 186     it 'should add properties to the map' do
> 187       map             = RentalMap.new @map_name, @latitude, @longitude
> 188       mock_property1  = mock 'property'
> 189       mock_property2  = mock 'property'
> 190
> 191       Property.stub!(:find).and_return mock_property1, mock_property2

This should be:

  Property.stub!(:find).and_return [mock_property1, mock_property2]

The way you had it returns mock_property1 the first time Property is
sent :find and mock_property2 the 2nd.

Make sense?


> 192       map.stub!(:add_marker).and_return '?'
> 193
> 194       mock_property1.should_receive(:address  ).and_return '400 Bloor
> Street'
> 195       mock_property1.should_receive(:latitude ).and_return 12.34
> 196       mock_property1.should_receive(:longitude).and_return 56.78
> 197       mock_property2.should_receive(:address  ).and_return '500 Bloor
> Street'
> 198       mock_property2.should_receive(:latitude ).and_return 12.45
> 199       mock_property2.should_receive(:longitude).and_return 56.89
> 200       map.stub!(:generate_marker_contents).twice.and_return 'Some
> contents for the first property', 'Some contents for the second property'
> 201
> 202       map.should_receive(:add_marker).with mock_property1.address,
> mock_property1.latitude, mock_property1.longitude,
> map.generate_marker_contents
> 203       map.should_receive(:add_marker).with mock_property2.address,
> mock_property2.latitude, mock_property2.longitude,
> map.generate_marker_contents
> 204
> 205       map.add_properties
> 206     end
> 207   end
>
> However, this fails with:
>
> Spec::Mocks::MockExpectationError in 'RentalMap#add_properties should add
> properties to the map'
> Mock 'property' received unexpected message :each with (no args)
> /Users/nick/src/myapp.podoboo.com/app/models/rental_map.rb:167:in
> `add_properties'
> ./spec/models/rental_map_spec.rb:205:
>
> I don't think that I should be stubbing Array#each , but I'm not sure what I
> should change. Any suggestions?
>
> Thanks!
> Nick
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to