This project is archived and is in readonly mode.

#6097 ✓wontfix
Robert Dober

Testing Nested Routes

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

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

Comments and changes to this ticket

  • Robert Dober

    Robert Dober December 1st, 2010 @ 03:31 PM

    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
    
  • Andrew White

    Andrew White February 14th, 2011 @ 10:39 AM

    • 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.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages