简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM