Nick Hoffman
2008-Sep-25 17:47 UTC
[rspec-users] #stub! and #should_receive on the same method
I''m calling #stub! and #should_receive on the same method within a class, and am finding that the method doesn''t return the value given to #stub! 216 it ''should make a map marker'' do 217 mock_property = mock ''property'', 218 :address => ''400 Bloor Street'', 219 :title => ''Some Title'', 220 :latitude => 12.34, 221 :longitude => 56.78, 222 :is_a? => true 223 mock_marker = mock ''gmarker'' 224 225 RentalMap.stub!(:generate_marker_contents).and_return ''Some content'' 226 GMarker.stub!(:new).and_return mock_marker 227 GMarker.should_receive(:new).with \ 228 [mock_property.latitude, mock_property.longitude], 229 {:title => mock_property.title, :info_window => RentalMap.generate_marker_contents(mock_property)} 230 231 marker = RentalMap.make_marker(mock_property) 232 puts "marker is a [#{marker.class}]" 233 marker.should equal(mock_marker) 234 end I want to stub GMarker#new to return the mock GMarker on line 223. I also want to ensure that GMarker#new is being called once with certain arguments, as seen on lines 227-229. However, this spec fails, and the ''gmarker'' variable is nil: gmarker is a [NilClass] F. 1) NoMethodError in ''RentalMap#make_marker should make a map marker'' You have a nil object when you didn''t expect it! The error occurred while evaluating nil.a? ./spec/models/rental_map_spec.rb:233: If I comment out lines 227-229, the spec succeeds, and the ''marker'' variable is the expected mock object: marker = [Spec::Mocks::Mock] With lines 227-229 uncommented, why is the ''gmarker'' variable nil? Thanks! Nick
Tero Tilus
2008-Sep-25 17:58 UTC
[rspec-users] #stub! and #should_receive on the same method
2008-09-25 13:47, Nick Hoffman:> I''m calling #stub! and #should_receive on the same method within a > class, and am finding that the method doesn''t return the value given > to #stub!How about GMarker.should_receive(:new).with(foo).and_return mock_marker -- Tero Tilus ## 050 3635 235 ## http://www.tilus.net/koti/tero/
Nick Hoffman
2008-Sep-25 18:04 UTC
[rspec-users] #stub! and #should_receive on the same method
On 2008-09-25, at 13:58, Tero Tilus wrote:> 2008-09-25 13:47, Nick Hoffman: >> I''m calling #stub! and #should_receive on the same method within a >> class, and am finding that the method doesn''t return the value given >> to #stub! > > How about GMarker.should_receive(:new).with(foo).and_return > mock_markerI knew there was a dead simple answer to the question. Thanks, Tero. Apologies for the brain fart.
David Chelimsky
2008-Sep-25 18:04 UTC
[rspec-users] #stub! and #should_receive on the same method
On Thu, Sep 25, 2008 at 12:47 PM, Nick Hoffman <nick at deadorange.com> wrote:> I''m calling #stub! and #should_receive on the same method within a class, > and am finding that the method doesn''t return the value given to #stub! > > 216 it ''should make a map marker'' do > 217 mock_property = mock ''property'', > 218 :address => ''400 Bloor Street'', > 219 :title => ''Some Title'', > 220 :latitude => 12.34, > 221 :longitude => 56.78, > 222 :is_a? => true > 223 mock_marker = mock ''gmarker'' > 224 > 225 RentalMap.stub!(:generate_marker_contents).and_return ''Some > content'' > 226 GMarker.stub!(:new).and_return mock_marker > 227 GMarker.should_receive(:new).with \ > 228 [mock_property.latitude, mock_property.longitude], > 229 {:title => mock_property.title, :info_window => > RentalMap.generate_marker_contents(mock_property)} > 230 > 231 marker = RentalMap.make_marker(mock_property) > 232 puts "marker is a [#{marker.class}]" > 233 marker.should equal(mock_marker) > 234 end > > I want to stub GMarker#new to return the mock GMarker on line 223. I also > want to ensure that GMarker#new is being called once with certain arguments, > as seen on lines 227-229. > > However, this spec fails, and the ''gmarker'' variable is nil: > gmarker is a [NilClass] > F. > 1) > NoMethodError in ''RentalMap#make_marker should make a map marker'' > You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.a? > ./spec/models/rental_map_spec.rb:233: > > If I comment out lines 227-229, the spec succeeds, and the ''marker'' variable > is the expected mock object: > marker = [Spec::Mocks::Mock] > > With lines 227-229 uncommented, why is the ''gmarker'' variable nil?Do you have the latest code from git? If so this should work as expected.> > Thanks! > Nick > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
David Chelimsky
2008-Sep-25 18:09 UTC
[rspec-users] #stub! and #should_receive on the same method
On Thu, Sep 25, 2008 at 1:04 PM, Nick Hoffman <nick at deadorange.com> wrote:> On 2008-09-25, at 13:58, Tero Tilus wrote: >> >> 2008-09-25 13:47, Nick Hoffman: >>> >>> I''m calling #stub! and #should_receive on the same method within a >>> class, and am finding that the method doesn''t return the value given >>> to #stub! >> >> How about GMarker.should_receive(:new).with(foo).and_return mock_marker > > I knew there was a dead simple answer to the question. Thanks, Tero.That should work too, but recent changes support returning a previously defined stub value when you don''t specify a return value in a mock expectation: foo = mock(''foo'') foo.stub!(:bar).and_return(''stub value'') foo.should_receive(:bar).with(''anything'') foo.bar(''anything'') => ''stub value''> Apologies for the brain fart. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Tero Tilus
2008-Sep-25 18:10 UTC
[rspec-users] #stub! and #should_receive on the same method
2008-09-25 14:04, Nick Hoffman:> I knew there was a dead simple answer to the question. Thanks, Tero. > Apologies for the brain fart.Np. Done the same quite a few times myself. ;) -- Tero Tilus ## 050 3635 235 ## http://www.tilus.net/koti/tero/
Nick Hoffman
2008-Sep-25 18:34 UTC
[rspec-users] #stub! and #should_receive on the same method
On 2008-09-25, at 14:04, David Chelimsky wrote:> On Thu, Sep 25, 2008 at 12:47 PM, Nick Hoffman <nick at deadorange.com> > wrote: >> I''m calling #stub! and #should_receive on the same method within a >> class, >> and am finding that the method doesn''t return the value given to >> #stub! >> >> 216 it ''should make a map marker'' do >> 217 mock_property = mock ''property'', >> 218 :address => ''400 Bloor Street'', >> 219 :title => ''Some Title'', >> 220 :latitude => 12.34, >> 221 :longitude => 56.78, >> 222 :is_a? => true >> 223 mock_marker = mock ''gmarker'' >> 224 >> 225 RentalMap.stub!(:generate_marker_contents).and_return ''Some >> content'' >> 226 GMarker.stub!(:new).and_return mock_marker >> 227 GMarker.should_receive(:new).with \ >> 228 [mock_property.latitude, mock_property.longitude], >> 229 {:title => mock_property.title, :info_window => >> RentalMap.generate_marker_contents(mock_property)} >> 230 >> 231 marker = RentalMap.make_marker(mock_property) >> 232 puts "marker is a [#{marker.class}]" >> 233 marker.should equal(mock_marker) >> 234 end >> >> I want to stub GMarker#new to return the mock GMarker on line 223. >> I also >> want to ensure that GMarker#new is being called once with certain >> arguments, >> as seen on lines 227-229. >> >> However, this spec fails, and the ''gmarker'' variable is nil: >> gmarker is a [NilClass] >> F. >> 1) >> NoMethodError in ''RentalMap#make_marker should make a map marker'' >> You have a nil object when you didn''t expect it! >> The error occurred while evaluating nil.a? >> ./spec/models/rental_map_spec.rb:233: >> >> If I comment out lines 227-229, the spec succeeds, and the ''marker'' >> variable >> is the expected mock object: >> marker = [Spec::Mocks::Mock] >> >> With lines 227-229 uncommented, why is the ''gmarker'' variable nil? > > Do you have the latest code from git? If so this should work as > expected.Hi David. I installed the rspec and rspec-rails plugins in my Rails app on August 19, and haven''t updated them since. Was this functionality added after August 19? -Nick
David Chelimsky
2008-Sep-25 18:39 UTC
[rspec-users] #stub! and #should_receive on the same method
On Thu, Sep 25, 2008 at 1:34 PM, Nick Hoffman <nick at deadorange.com> wrote:> On 2008-09-25, at 14:04, David Chelimsky wrote: >> >> On Thu, Sep 25, 2008 at 12:47 PM, Nick Hoffman <nick at deadorange.com> >> wrote: >>> >>> I''m calling #stub! and #should_receive on the same method within a class, >>> and am finding that the method doesn''t return the value given to #stub! >>> >>> 216 it ''should make a map marker'' do >>> 217 mock_property = mock ''property'', >>> 218 :address => ''400 Bloor Street'', >>> 219 :title => ''Some Title'', >>> 220 :latitude => 12.34, >>> 221 :longitude => 56.78, >>> 222 :is_a? => true >>> 223 mock_marker = mock ''gmarker'' >>> 224 >>> 225 RentalMap.stub!(:generate_marker_contents).and_return ''Some >>> content'' >>> 226 GMarker.stub!(:new).and_return mock_marker >>> 227 GMarker.should_receive(:new).with \ >>> 228 [mock_property.latitude, mock_property.longitude], >>> 229 {:title => mock_property.title, :info_window => >>> RentalMap.generate_marker_contents(mock_property)} >>> 230 >>> 231 marker = RentalMap.make_marker(mock_property) >>> 232 puts "marker is a [#{marker.class}]" >>> 233 marker.should equal(mock_marker) >>> 234 end >>> >>> I want to stub GMarker#new to return the mock GMarker on line 223. I also >>> want to ensure that GMarker#new is being called once with certain >>> arguments, >>> as seen on lines 227-229. >>> >>> However, this spec fails, and the ''gmarker'' variable is nil: >>> gmarker is a [NilClass] >>> F. >>> 1) >>> NoMethodError in ''RentalMap#make_marker should make a map marker'' >>> You have a nil object when you didn''t expect it! >>> The error occurred while evaluating nil.a? >>> ./spec/models/rental_map_spec.rb:233: >>> >>> If I comment out lines 227-229, the spec succeeds, and the ''marker'' >>> variable >>> is the expected mock object: >>> marker = [Spec::Mocks::Mock] >>> >>> With lines 227-229 uncommented, why is the ''gmarker'' variable nil? >> >> Do you have the latest code from git? If so this should work as expected. > > Hi David. I installed the rspec and rspec-rails plugins in my Rails app on > August 19, and haven''t updated them since. Was this functionality added > after August 19?Yep - Sept 17: http://github.com/dchelimsky/rspec/commit/dab567d187b89bae35c3c822f413928c1f9b046e> -Nick > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >