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

Anthony Carlos anthony at digitalphenom.com
Fri Jun 15 14:46:08 EDT 2007


Ingo:

I cannot find any documentation on the :all parameter to the "with"  
clause. Can you try it without the "with" clause?

Station.should_receive(:find).and_return(@mock_stations)

See if it goes through and let me know what happens.

-Anthony


On Jun 15, 2007, at 1:22 PM, Ingo Weiss wrote:

> Ah, I see how it makes sense to do it in this order. However, the
> error does not go away when I state the expectation before issuing
> the request. I will keep investigating...
>
> Ingo
>
>
> On Jun 14, 2007, at 8:48 PM, Anthony Carlos wrote:
>
>> 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
>>
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list