[rspec-users] how can this pass?

Mark Wilden mark at mwilden.com
Sun Feb 8 21:20:00 EST 2009


On Sun, Feb 8, 2009 at 5:35 PM, Sergio Bayona <lists at ruby-forum.com> wrote:

> how could this test pass?
>
> I have:
>
> class PropertiesController < ApplicationController
>  def show
>    @property = Property.non_existing_method #causes a method missing
> error
>  end
> end
>
>
> describe PropertiesController do
>  def mock_property(stubs={})
>      @mock_property ||= mock_model(Property, stubs)
>  end
>
>  describe "responding to GET show" do
>    it "should expose the requested property as @property" do
>
>  Property.should_receive(:non_existing_method).and_return(mock_property)
>        get :show, :id => "37"
>        assigns[:property].should equal(mock_property)
>    end
>  end
> end
>
> SB-MacBook-Pro:test sb$ ruby
> ./spec/controllers/properties_controller_spec.rb
> .
>
> Finished in 0.134667 seconds
>
> 1 example, 0 failures
>
>
> why zero failures? more specifically, why would :non_existing_method
> return mock_property? and why would the assigns[:property] be equal to
> mock_property? did I eat some bad mushrooms?
>

That's just the way this mock library works. It just makes sure it receives
a particular message and then returns a certain value in response. It
doesn't actually check to see whether the mocked object responds to that
message. Given Ruby's dynamic nature, that check might not even be valid at
the time.

Of course, this can be a source of false positives. That's what you get when
you test behavior instead of state.

///ark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20090208/8b6bd108/attachment.html>


More information about the rspec-users mailing list