[rspec-users] problem getting started with stubbing in rails controller specs

Anthony Carlos anthony at digitalphenom.com
Thu Jun 14 20:48:52 EDT 2007


Hello, Ingo!

I'm copying your question to a new e-mail instead of replying to try  
to keep message threads separate.

Have you tried setting up your expectation before issuing the get  
statement? For example, take this out of the setup block:
get 'index', :locale => 'en'

and move it after:
Station.should_receive(:find).with(:all).and_return(@mock_stations)

I set-up inter-object expectations before exercising the controller  
action.

Does this help?

-Anthony

PS: If you could be more specific about which concepts about mocks  
and stubs are confusing you, I'd be happy to try and help.l

----------

Hi,

I am getting started with writing controller specs with rspec/rails,
and seem to have trouble fully grasping the concept of mocks and
stubs. I can't figure out what's wrong with the following. This is
the action I want to spec:

    def index
      @stations = Station.find(:all, :order => 'name')
    end

and this is my spec:


describe "GET to index" do
    controller_name :stations

    before(:each) do
      @mock_station = mock_model(Station)
      @mock_stations = [@mock_station]
      Station.stub!(:find).and_return(@mock_stations)
      get 'index', :locale => 'en'
    end

    it "should find stations" do
       Station.should_receive(:find).with(:all).and_return
(@mock_stations)
    end

end


It fails with this error:

Spec::Mocks::MockExpectationError in 'GET to index should find stations'
Station expected :find with (:all) once, but received it 0 times
./spec/controllers/stations_controller_spec.rb:22:

I can see in the log that the database is not hit for Station.find
(:all), so the stubbing seems to have worked. Guess I'm stuck. Thanks
for any hint to get me going again!

Ingo




_______________________________________________
rspec-users mailing list
rspec-users at rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list