[rspec-users] 2nd attempt at mocking and stubbing

Nick Hoffman nick at deadorange.com
Sat Sep 6 17:48:19 EDT 2008


On 2008-09-06, at 15:58, David Chelimsky wrote:
> Well, without changing the underlying semantics, you can clean up the
> syntax a bit like this:
>
>  mock_property1 = stub('property', :address => '400 Bloor Street',
> :latitude => 12.34, :longitude => 56.78)
>
> Of course, that doesn't address your question :)

That makes the specs much easier to read!

> Here's another thought that doesn't answer your question - this line
> in the code:
>
>  add_marker p.address, p.latitude, p.longitude,  
> generate_marker_contents(p)
>
> is resulting in a situation where you have to do a lot of setup in
> order to isolate the object under test. The line exhibits the Feature
> Envy Code Smell, which is the odor emitted by one object depending on
> another object's data.

RentalMap#add_properties does have feature envy. However...

> If I were coding by example, I'd probably start w/ a simple example  
> like this:
>
> describe '#add_properties' do
>  it 'should add one property to the map' do
>    map = RentalMap.new @map_name, @latitude, @longitude
>    mock_property = mock 'property'
>    Property.stub!(:find).and_return [mock_property]
>
>    mock_property.should_receive(:add_marker_to).with(map)
>
>    map.add_properties
>  end
> end
>
> This defines how I'd like to be able to "talk" to the Property, and
> results in an implementation like this:
>
>  p.add_marker_to(map)
>
> Now all the additional behaviour is pushed to the Property, for which
> you can write code examples with the details of its own data rather
> than having to stub so much of its data for use in the current
> example.

I believe that the process of adding a marker (IE: Property) to a  
RentalMap should be a feature of the RentalMap model, rather than the  
Property model. From a Property's perspective, a property has nothing  
to do (IE: no interactions) with RentalMaps. From a RentalMap's  
perspective, a RentalMap has lots to do with Properties, because it  
needs to ask a Property for information so that it can add a marker to  
itself.

To make an analogy, do I put on a shirt, or does a shirt put itself on  
me?

Thanks again for your opinions, David!
Nick


More information about the rspec-users mailing list