Forum: RSpec mock weirdness

C9669e4a2874af592652f022e5b55810?d=identicon&s=25 Nikolay Sturm (Guest)
on 2011-08-22 12:14
(Received via mailing list)
Hi,

I have a strange problem with mocking an object that has a method called
'load'. With Rails 2.3 and rspec-rails 1.3 I could do sth like this:

describe Foo do
  let(:bar) { mock(Bar).as_null_object }
  before(:each) do
    Bar.stub(:new).and_return(bar)
  end

  it 'does something' do
    Foo.do_something
  end
end

with

class Foo
  def do_something
    bar = Bar.new
    bar.load
  end
end

After an upgrade to rails 3.0.10 and rspec-rails 2.6.1 I get an
ArgumentError:
  wrong number of arguments (0 for 1)

If I add "bar.method(:load)" to do_something(), it prints

#<Method:
RSpec::Mocks::Mock(ActiveSupport::Dependencies::Loadable)#load>

which doesn't look right to me. I am using ree-1.8.7-2011.03.

Does anyone have an idea what this is about and how to properly deal
with this?

cheers,

Nikolay

--
"It's all part of my Can't-Do approach to life." Wally
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2011-08-22 15:19
(Received via mailing list)
On Aug 22, 2011, at 5:11 AM, Nikolay Sturm wrote:

>
>    bar.load
>  end
> end
>
> After an upgrade to rails 3.0.10 and rspec-rails 2.6.1 I get an
> ArgumentError:
>  wrong number of arguments (0 for 1)
>
> If I add "bar.method(:load)" to do_something(), it prints
>
> #<Method: RSpec::Mocks::Mock(ActiveSupport::Dependencies::Loadable)#load>

This ^^ tells you everything you need to know to research the problem.

If you look at the code for ActiveSupport::Dependencies
(https://github.com/rails/rails/blob/v3.0.10/active...),
you'll see that the Loadable module is included in every object. See:

  https://github.com/rails/rails/blob/v3.0.10/active...
  https://github.com/rails/rails/blob/v3.0.10/active...

This means that even mock objects already have a load method, which is
defined to accept one or more arguments:

  https://github.com/rails/rails/blob/v3.0.10/active...

This means that you need to explicitly stub the load method to do what
you're trying to do:

  let(:bar) { mock(Bar, :load => nil).as_null_object }

HTH,
David
C9669e4a2874af592652f022e5b55810?d=identicon&s=25 Nikolay Sturm (Guest)
on 2011-08-22 16:08
(Received via mailing list)
* David Chelimsky [2011-08-22]:
> This means that you need to explicitly stub the load method to do what
> you're trying to do:
>
>   let(:bar) { mock(Bar, :load => nil).as_null_object }

Thanks for the explanation, David. I wasn't sure whether or not this was
accepted behaviour, I would have expected a null object to be, well, an
object without methods. At least the next guy's google search will point
to the solution. :)

cheers,

Nikolay
This topic is locked and can not be replied to.