[rspec-users] stub().and_raise() ain't raising?

Andrew Premdas apremdas at gmail.com
Fri Jan 13 02:38:19 EST 2012

On 13 January 2012 05:18, Fearless Fool <lists at ruby-forum.com> wrote:
> Justin Ko wrote in post #1040632:
>> What are the failed expectations? Just paste it.
> You've identified the problem (see below), but to answer your question:
>  1) Wizard electricity credentials with valid credentials should spawn
> external loader
>     Failure/Error: @wizard.electricity_loader.should_receive(:start)
>       (#<Wizard::ElectricityLoader:0x000001029bc140>).start(any args)
>           expected: 1 time
>           received: 0 times
>     # ./spec/models/wizard_spec.rb:181:in `block (4 levels) in <top
> (required)>'
>> Are you sure that @wizard.electricity_service is returning the same
>> object on each call? If a database call is being made, there's a good
>> chance it is returning a different object (which has not mocks).
> Ah!  I think that's the culprit -- @wizard.electricity_service invokes a
> db call:
> class Wizard < ActiveRecord::Base
>  def electricity_service
>    premise.metered_services.where(:natural_resource_id =>
> NaturalResource::ELECTRICITY.id)
>  end
> end

I think your tests are telling you to refactor your code. the
`premise.metered_services.where` is breaking the law of demeter. This
makes it hard to stub/mock. I'd also question the naming of the Wizard
class. One thing to consider doing is running the tests with the
pretty output and reading it.

So why should Wizard#electricity_service spawn an external loader? Why
should a Wizard have an electricity service etc.

There ia a tendency when writing specs, to focus on getting the spec
to pass, and doing complicated things to make this happen. But really
its much more important to listen to the tests to make things clear
and simple.

In this case perhaps there is an ElectricityService class asking to be born.



> It seems obvious now that you point it out.  So in rspec-land, what's
> the stylistically appropriate way to test this?  Stub .any_class()?
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Andrew Premdas

More information about the rspec-users mailing list