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

Ingo Weiss ingo at ingoweiss.com
Thu Jun 14 16:43:01 EDT 2007


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






More information about the rspec-users mailing list