views:

634

answers:

2

While learning Rails I've created an application with a Domains controller nested below a Customers controller. I'm using Rails 2.3.4 and it's been a learning experience. I managed to get the below routing set up:

    customer_domains GET    /customers/:customer_id/domains(.:format)          {:controller=>"domains", :action=>"index"}
                     POST   /customers/:customer_id/domains(.:format)          {:controller=>"domains", :action=>"create"}
 new_customer_domain GET    /customers/:customer_id/domains/new(.:format)      {:controller=>"domains", :action=>"new"}
edit_customer_domain GET    /customers/:customer_id/domains/:id/edit(.:format) {:controller=>"domains", :action=>"edit"}
     customer_domain GET    /customers/:customer_id/domains/:id(.:format)      {:controller=>"domains", :action=>"show"}
                     PUT    /customers/:customer_id/domains/:id(.:format)      {:controller=>"domains", :action=>"update"}
                     DELETE /customers/:customer_id/domains/:id(.:format)      {:controller=>"domains", :action=>"destroy"}
           customers GET    /customers(.:format)                               {:controller=>"customers", :action=>"index"}
                     POST   /customers(.:format)                               {:controller=>"customers", :action=>"create"}
        new_customer GET    /customers/new(.:format)                           {:controller=>"customers", :action=>"new"}
       edit_customer GET    /customers/:id/edit(.:format)                      {:controller=>"customers", :action=>"edit"}
            customer GET    /customers/:id(.:format)                           {:controller=>"customers", :action=>"show"}
                     PUT    /customers/:id(.:format)                           {:controller=>"customers", :action=>"update"}
                     DELETE /customers/:id(.:format)                           {:controller=>"customers", :action=>"destroy"}
                root        /                                                  {:controller=>"customers", :action=>"index"}

However, all tests for the Domains controller are failing due to routing errors.

For example the following test (generated by Rails' resource generator) fails, as do all other tests in the DomainsControllerTest class.

class DomainsControllerTest < ActionController::TestCase
  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:domains)
  end
end

It fails with the error:

No route matches {:action => "index", :controller => "domains"}

This makes sense since the default routes no longer exist and the Domains controller requires a @customer to be set. I've spent an afternoon looking for the needed change, but almost every site talks about Rspec tests instead of regular Rails tests.

How do I modify the domains_controller_test.rb so it will understand the nested resource?

+3  A: 

Passing the customer_id with the requests would do. Something like this :-

class DomainsControllerTest < ActionController::TestCase
  test "should get index" do
    get :index ,:customer_id=> 1
    assert_response :success
    assert_not_nil assigns(:domains)
  end
end
Rishav Rastogi
That seems to work, Rishav. Is there any way to DRY this up, instead of passing the id to each separate test?
Martijn Heemels
Martijn, that's what you're gonna have to do. Your routing must know the customer to work. I suppose you could write a test helper to replace the "get" call that autofills the customer_id part of the hash, but that seems like a bad idea unless you're writing many, many tests against the DomainsController.
Barry Hess
A: 

Domains no longer exist outside of the context of a customer. The request needs a customer for the named routes to match.

The way to do this in your test is:

test "should get index" do get :index, :customer_id=>joe

ndp