Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Testing Nested Routes

#6097

It seems that neither rspec nor test/unit can see nested routes.
The problem was described here first.

I have pushed a minimal application to github which should allow to reproduce this with rspec. https://github.com/RobertDober/BitsAndPieces/tree/master/checkroute...

 rspec spec/controllers/blogs_*

will work, but the embedded

 rspec spec/controllers/blogposts_*

will not

Reported by Robert Dober · December 1st, 2010 @ 02:43 PM

State: wontfix
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Robert Dober
    Robert Dober

    Just to be a little bit clearer, I suggest something like this to be generated by e.g.
    rspec:

    
    describe BlogpostsController do
    
      def mock_blog
        @mock_blog ||= Object.new # or somerhing more elaborate maybe?
      end
    
      def mock_blogpost(stubs={})
        (@mock_blogpost ||= mock_model(Blogpost).as_null_object).tap do |blogpost|
          blogpost.stub(stubs) unless stubs.empty?
        end
      end
    
      describe "GET index" do
        it "assigns all blogposts as @blogposts" do
          Blog.stub(:find){ mock_blog }
          Blogpost.stub(:all) { [mock_blogpost] }
          get :index, :blog_id => 42
          assigns(:blogposts).should eq([mock_blogpost])
          assigns(:blog).should eq(mock_blog)
        end
      end
    

    December 1st, 2010 @ 03:31 PM

  2. Andrew White
    Andrew White
    • State changed from new to wontfix
    • Importance changed from to Low

    The following works fine for me in Test::Unit and RSpec

    # config/routes.rb
    resources :manufacturers, :only => :show do
      resources :products, :only => :index
    end
    
    # test/functional/products_controller_test.rb
    require 'test_helper'
    
    class ProductsControllerTest < ActionController::TestCase
      test "should get index" do
        get :index, :manufacturer_id => 1
        assert_response :success
      end
    end
    
    # specs/controller/products_controller_spec.rb
    require 'spec_helper'
    
    describe ProductsController do
      describe "GET index" do
        it "has a 200 status code" do
          get :index, :manufacturer_id => 1
          response.code.should eq("200")
        end
      end
    end
    

    Are you complaining that the auto generated scaffold tests are not taking account of the nested route? If so I think that's asking a bit much of something that's meant just to get you started on writing your tests. What about where a controller is used both in a nested and non-nested scope, e.g:

    resources :manufacturers, :only => :show do
      resources :products, :only => :index
    end
    
    resources :products, :only => [:index, :show]
    

    In this case you need two tests. As I said scaffold is just a guide to get you started - it's not there to build your application for you.

    February 14th, 2011 @ 10:39 AM