Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec-rails: Failure/Error: get "/" No route matches

Trying out rspec-rails. I get a weird error - no routes are supposedly found, even though I can access them fine in the browser when running rails s.

I even tried it with just /

Failure/Error: get "/"
     ActionController::RoutingError:
       No route matches {:controller=>"action_view/test_case/test", :action=>"/"}

I can definitely access / and other resources in the browser, though. Is there something I could've missed when setting rspec up? I put it into the Gemfile and ran rspec:install.

Thank you, MrB

edit: Here's my test

  1 require 'spec_helper'
  2 
  3 describe "resource" do
  4   describe "GET" do
  5     it "contains /" do
  6       get "/"
  7       response.should have_selector("h1", :content => "Project")
  8     end
  9   end
 10 end

Here's my route file:

myApp::Application.routes.draw do

  resources :groups do
    resources :projects
  end 

  resources :projects do
    resources :variants
    resources :steps

    member do
      get 'compare'
    end 
  end 

  resources :steps do
    resources :costs
  end 

  resources :variants do
    resources :costs
  end 

  resources :costs

  root :to => "home#index"

end

My spec_helper.rb:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'    

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|

  config.mock_with :rspec
  config.include RSpec::Rails::ControllerExampleGroup


  config.fixture_path = "#{::Rails.root}/spec/fixtures"


  config.use_transactional_fixtures = true
end

Didn't really change anything here, I think.

like image 929
MrB Avatar asked Apr 27 '11 13:04

MrB


2 Answers

As far as i know, you are trying to combine two tests into one. In rspec this should be solved in two steps. In one spec you test the routing, and in another you test the controller.

So, add a file spec/routing/root_routing_spec.rb

require "spec_helper"

describe "routes for Widgets" do
  it "routes /widgets to the widgets controller" do
    { :get => "/" }.should route_to(:controller => "home", :action => "index")
  end
end

And then add a file spec/controllers/home_controller_spec.rb, and I am using the extended matchers defined by shoulda or remarkable.

require 'spec_helper'

describe HomeController do

  render_views

  context "GET index" do
    before(:each) do
      get :index
    end
    it {should respond_with :success }
    it {should render_template(:index) }

    it "has the right title" do
      response.should have_selector("h1", :content => "Project")
    end

  end
end  

Actually, I almost never use render_views but always test my components as isolated as possible. Whether the view contains the correct title I test in my view-spec.

Using rspec i test each component (model, controller, views, routing) separately, and i use cucumber to write high level tests to slice through all layers.

Hope this helps.

like image 176
nathanvda Avatar answered Sep 20 '22 21:09

nathanvda


You have to describe a controller for a controller test. Also, since you're testing the contents of the view in the controller test and not a separate view spec, you have to render_views.

describe SomeController, "GET /" do
  render_views

  it "does whatever" do
    get '/'
    response.should have_selector(...)
  end
end
like image 37
Robert Speicher Avatar answered Sep 22 '22 21:09

Robert Speicher