[rspec-users] Controller specs for sub-controllers

Pat Maddox patmaddox at me.com
Wed Dec 8 12:56:41 EST 2010


On Dec 6, 2010, at 4:29 AM, Dean wrote:

> I'm completely new to RSpec (and fairly new to Rails, too.) I'm
> working on an existing application that has an Admin::BaseController
> and sub-controllers such as Admin:TestimonialsController:
> 
>  class Admin::BaseController < ApplicationController
>  end
> 
>  class Admin::TestimonialsController < Admin::BaseController
> 
>    def new
>      @page_title = "Create New Testimonial"
>    end
>  end
> 
> I'm trying to write very basic tests for my Testimonials controller,
> like so:
> 
> [testimonials_controller_spec.rb]
> 
> require 'spec_helper'
> 
> describe Admin::TestimonialsController do
>  integrate_views
> 
>  describe "GET 'new'" do
>    it "should be successful" do
>      get 'new'
>      response.should be_success
>    end
> 
>    it "should have the right title" do
>      get 'testimonials/new'
>      response.should have_tag("title", /Create New Testimonial/)
>    end
>  end
> end
> 
> I've created the 'new' view and it gets the right title tag, but the
> tests fail:
> 
> 1)
> 'Admin::TestimonialsController GET 'new' should render the create
> testimonial template' FAILED
> expected success? to return true, got false
> /Users/Lowell/rails_projects/genlighten_dr/spec/controllers/admin/
> testimonials_controller_spec.rb:9:
> 
> 2)
> 'Admin::TestimonialsController GET 'new' should have the right title'
> FAILED
> Expected at least 1 element matching "title", found 0.
> <false> is not true.
> /Users/Lowell/rails_projects/genlighten_dr/spec/controllers/admin/
> testimonials_controller_spec.rb:14:
> 
> I suspect it has something to do with the path to my views. Because
> the testimonials sub-controller inherits from the admin
> BaseController, both testimonials controllers and views are in
> subdirectories, e.g.:
> 
> app/controllers/admin/testimonials_controller.rb
> 
> and
> 
> app/views/admin/testimonials/new.html.erb
> 
> Should my tests say something like:
> 
> get 'admin/testimonials/new' instead of get 'new'? Or am I making a
> less subtle mistake?
> 
> Thanks,
> 
> Dean
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

No the path shouldn't matter.  When you do
get 'new'

you're saying, "make a GET request to the 'new' action of this controller" -- it has nothing to do with layouts.

My two guesses is that there's a 500 error somewhere, or you have a filter that is redirecting a login page and need to do some setup in order to get past it.

To check the first guess, tail -f log/test.log when you run your test.  See if there's an exception backtrace in there.

To check the second guess, inspect the response to see if it's a redirect or something else.

Pat


More information about the rspec-users mailing list