How to test controllers with nested routes using Rspec?

Viewed 7372

I have 2 controllers that I created using scaffold generator of rails. I wanted them to be nested in a folder called "demo" and so ran

rails g scaffold demo/flows
rails g scaffold demo/nodes

Then I decided to nest nodes inside flows, and changed my routes file like so:

namespace :demo do 
  resources :flows do
    resources :nodes
  end
end

But this change resulted on the rspec tests for nodes breaking with ActionController::Routing errors.

  15) Demo::NodesController DELETE destroy redirects to the demo_nodes list
     Failure/Error: delete :destroy, :id => "1"
     ActionController::RoutingError:
       No route matches {:id=>"1", :controller=>"demo/nodes", :action=>"destroy"}

The problem is rspec is looking at the wrong route. It's supposed to look for "demo/flows/1/nodes". It also needs a mock model for flow, but I am not sure how to provide that. Here is my sample code from generated rspec file:

  def mock_node(stubs={})
    @mock_node ||= mock_model(Demo::Node, stubs).as_null_object
  end

  describe "GET index" do
    it "assigns all demo_nodes as @demo_nodes" do
      Demo::Node.stub(:all) { [mock_node] }
      get :index
      assigns(:demo_nodes).should eq([mock_node])
    end
  end

Can someone help me understand how I need to provide the flow model?

1 Answers
Related