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

Trying rspec rails. I get a strange error: no routes were allegedly found, although I can access them in the browser when running s rails.

I even tried this with just

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

I can definitely access other resources in the browser. Is there something I could skip when installing rspec? I put it in a gemfile and ran rspec: install.

Thank you CMO

edit: Here is 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 is 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 

I don’t change anything here, I think.

+6
source share
2 answers

As far as I know, you are trying to combine two tests into one. In rspec, this should be allowed in two steps. In one specifier, you check the routing, and in the other you check the controller.

So add 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 the spec/controllers/home_controller_spec.rb , and I use advanced matches defined by position or wonderful.

 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 

In fact, I almost never use render_views , but I always test my components as isolated as possible. Is the view the correct title I'm testing in my view specification.

Using rspec i, I test each component (model, controller, views, routing) separately, and I use a cucumber to record high-level tests to trim all layers.

Hope this helps.

+4
source

You must have describe controller to check the controller. In addition, since you are checking the contents of a view in a controller test, and not a separate view specification, you should render_views .

 describe SomeController, "GET /" do render_views it "does whatever" do get '/' response.should have_selector(...) end end 
+2
source

Source: https://habr.com/ru/post/886798/


All Articles