[rspec-users] Testing Layout

Bart Zonneveld loop at superinfinite.com
Wed Apr 16 11:20:00 EDT 2008


On 16-apr-2008, at 16:53, Andy Croll wrote:
> Bart Zonneveld wrote:
>> On 16-apr-2008, at 13:57, Andy Croll wrote:
>>> Is there a way to specify which layout an action should use in a
>>> controller?
>>
>> Off the top of my head, not tested:
>>
>> describe MyController do
>>    it "should use the foo layout" do
>>      MyController.should_receive(:layout).with("foo")
>>      load("app/controllers/my_controller")
>>    end
>> end
>>
>> but I'm sure there's a more BDD way to do it (TIMTOWTDI goes  
>> BDDWTDI ?).
>>
>> greetz,
>> bartz
>
>
> Tried it. No dice it seems
>
> So I then tried:
>
>   it "should use the admin layout" do
>     controller.should_receive(:layout).with("admin")
>   end
>
> And I get...
>
> 1)
> Spec::Mocks::MockExpectationError in 'Admin::ProductsController should
> use the admin layout'
> Mock 'Admin::ProductsController' expected :layout with ("admin") once,
> but received it 0 times
>
> I'm not doing the load ... command you suggested. What is the thinking
> behind that? I'm finding that testing using RSpec demands you  
> understand
> a lot more of what is going on inside the framework!

That kind of makes sense, since should_receive's should be defined  
*before* something happens. So, slightly diverting here, but say a  
Post should receive find(:all) when you go to index/, it would look  
something like:

describe PostsController do
   it "should find all posts" do
     Post.should_receive(:find).with(:all)
     get "index"
   end
end

The "trick" I gave you above was based on this one: http:// 
www.nabble.com/Testing-that-a-model-helper-method-is-called- 
td15856651.html

Perhaps you should try giving the full path to load, eg. load 
(RAILS_ROOT + "/app/controllers/my_controller.rb").

greetz,
bartz


More information about the rspec-users mailing list