#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

2008-09-25 13:47, Nick H.:

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

On Thu, Sep 25, 2008 at 12:47 PM, Nick H. [email protected]
wrote:

223 mock_marker = mock ‘gmarker’
232 puts “marker is a [#{marker.class}]”
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.

On 2008-09-25, at 14:04, David C. wrote:

219 :title => ‘Some Title’,
229 {:title => mock_property.title, :info_window =>
arguments,

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

On Thu, Sep 25, 2008 at 1:34 PM, Nick H. [email protected]
wrote:

218 :address => ‘400 Bloor Street’,
228 [mock_property.latitude, mock_property.longitude],
arguments,

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

On Thu, Sep 25, 2008 at 1:04 PM, Nick H. [email protected]
wrote:

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’

On 2008-09-25, at 13:58, Tero T. wrote:

2008-09-25 13:47, Nick H.:

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.
Apologies for the brain fart.

2008-09-25 14:04, Nick H.:

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. :wink: