[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:

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

Does this help?


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



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')

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]
      get 'index', :locale => 'en'

    it "should find stations" do


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

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!


rspec-users mailing list
rspec-users at rubyforge.org

More information about the rspec-users mailing list