I am using mock_model in my tests, and I encountered a situation where my stub is failing. Here''s the scenario: Controller: @locations = Location.al(:bounds=>bounds)l @locations.sort_by_distance_from([bounds.center.lat,bounds.center.lng]) Rspec: @location = mock_model(Location) Location.stub(:all).and_return([@location]) @location.stub!(:sort_by_distance_from).and_return([@location]) Within real model: Location act_as_mappable .... the :sort_by_distance_from is a method from within act_as_mappable. Upon investigation, when I compare the available methods for both the Location model and the mock_model, I found act_as_mappable is missing from the mock_model. Which accounts for why it was failing in the stub @location.stub!(:sort_by_distance_from). It gives an error "unexpected message :distance_to" and the :distance_to method comes from act_as_mappable. When I switch to using Factory for the @location instance, the test passed. How should I go about using the mock_model for a situation as this, without resorting to using Factory? I believe this issue also the same for other act_as_ situations because I also noted that a number of my act_as.. are not available in the mock_model as they are in the actual model. Thanks for the help Steve -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120803/50acf9ed/attachment.html>
David Chelimsky
2012-Aug-03 14:28 UTC
[rspec-users] Mock_model not recognizing act_as_mappable
On Fri, Aug 3, 2012 at 9:33 AM, Steve Loo <isignin at gmail.com> wrote:> I am using mock_model in my tests, and I encountered a situation where my > stub is failing.You want stub_model for this case because you''re counting on functionality that is built into your real model. See https://www.relishapp.com/rspec/rspec-rails/docs/mocks/mock-model and https://www.relishapp.com/rspec/rspec-rails/docs/mocks/stub-model for more info.> Here''s the scenario: > > Controller: > > @locations = Location.al(:bounds=>bounds)l > > @locations.sort_by_distance_from([bounds.center.lat,bounds.center.lng]) > > > Rspec: > > @location = mock_model(Location) > > Location.stub(:all).and_return([@location]) > > @location.stub!(:sort_by_distance_from).and_return([@location]) > > > Within real model: Location > > act_as_mappable > > .... > > > the :sort_by_distance_from is a method from within act_as_mappable. > > Upon investigation, when I compare the available methods for both the > Location model and the mock_model, I found act_as_mappable is missing from > the mock_model. > > Which accounts for why it was failing in the stub > @location.stub!(:sort_by_distance_from). It gives an error "unexpected > message :distance_to" and the :distance_to method comes from > act_as_mappable. > > > When I switch to using Factory for the @location instance, the test passed. > > How should I go about using the mock_model for a situation as this, without > resorting to using Factory? I believe this issue also the same for other > act_as_ situations because I also noted that a number of my act_as.. are not > available in the mock_model as they are in the actual model. > > > Thanks for the help > > Steve > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users