[rspec-users] Controller specs for sub-controllers
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
> class Admin::TestimonialsController < Admin::BaseController
> def new
> @page_title = "Create New Testimonial"
> I'm trying to write very basic tests for my Testimonials controller,
> like so:
> require 'spec_helper'
> describe Admin::TestimonialsController do
> describe "GET 'new'" do
> it "should be successful" do
> get 'new'
> response.should be_success
> it "should have the right title" do
> get 'testimonials/new'
> response.should have_tag("title", /Create New Testimonial/)
> I've created the 'new' view and it gets the right title tag, but the
> tests fail:
> 'Admin::TestimonialsController GET 'new' should render the create
> testimonial template' FAILED
> expected success? to return true, got false
> 'Admin::TestimonialsController GET 'new' should have the right title'
> Expected at least 1 element matching "title", found 0.
> <false> is not true.
> 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.:
> Should my tests say something like:
> get 'admin/testimonials/new' instead of get 'new'? Or am I making a
> less subtle mistake?
> rspec-users mailing list
> rspec-users at rubyforge.org
No the path shouldn't matter. When you do
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.
More information about the rspec-users