error test routing with rspec - ruby-on-rails

I have this resource in my config/routes.rb
resources :users, :path => "u" do
resources :boards, :controller => 'users/boards', :path => "collections"
end
In my spec/routing/boards_routing_spec.rb i have:
require "spec_helper"
describe Users::BoardsController do
describe "routing" do
it "routes to #index" do
get("/u/:user_id/collections").should route_to("users/boards#index")
end
end
end
I get the next error:
Failures:
1) Users::BoardsController routing routes to #index
Failure/Error: get("/u/:user_id/collections").should route_to("users/boards#index")
The recognized options <{"action"=>"index", "controller"=>"users/boards", "user_id"=>":user_id"}> did not match <{"controller"=>"users/boards", "action"=>"index"}>, difference: <{"user_id"=>":user_id"}>.
<{"controller"=>"users/boards", "action"=>"index"}> expected but was
<{"action"=>"index", "controller"=>"users/boards", "user_id"=>":user_id"}>.
# ./spec/routing/boards_routing_spec.rb:7:in `block (3 levels) in <top (required)>'
I am using Devise 2.0 for user.
How can I fix this error?

The error was fixed:
require "spec_helper"
describe Users::BoardsController do
describe "routing" do
it "routes to #index" do
get("/u/hyperrjas/collections").should route_to("users/boards#index", :user_id => "hyperrjas")
end
end

Related

How to test session constrained routes with rspec routing specs?

I have such code in my routes.rb:
Rails.application.routes.draw do
authorized = ->(request) { request.session[:user_id].present? }
not_authorized = ->(request) { request.session[:user_id].blank? }
constraints authorized do
resources :users
end
constraints not_authorized do
get 'login' => 'auth#login_page'
post 'login' => 'auth#create_session'
get '*unmatched_route', to: 'auth#login_page'
root 'auth#login_page'
end
end
And I have such users_routing_spec.rb file:
require "rails_helper"
RSpec.describe UsersController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(:get => "/users").to route_to("users#index")
end
end
end
This test fails as it routes to 'auth#login_page' because there is no user_id
in session.
How can I call auth#create_session in advance to the expectation?
There is no request or #request object and I also can't make a manual request to 'auth#create_session'

Rspec route matching - same expected vs actual, yet error

1) ExampleController routes for Example routes
Failure/Error: expect( post: '/api/application').to route_to( format: 'json', action: 'create_application', controller: 'example')
The recognized options <{"format"=>"json", "action"=>"create_application", "controller"=>"example"}> did not
match <{"format"=>"json", "action"=>"create_application",
"controller"=>"example"}>,
difference:.
<{"format"=>"json", "action"=>"create_application",
"controller"=>"example"}>
expected but was
<{"format"=>"json", "action"=>"create_application",
"controller"=>"example"}>.
Am I missing something here? I feel as if expected and actual are the same.
#charles
routes.rb:
scope '/api', :defaults => { format: 'json' } do
resource :application, only:[] do
post :create, to: 'example#create_application'
end
end
rake routes:
application POST /api/application(.:format) example#create_application
{:format=>"json"}
Working now with:
describe "routes for Example" do
it "routes" do
expect( post: '/api/application').to route_to( "example#create_application", :format=>"json" )
end
end
I was Having same issue.
You just need to add hash in route_to function like.
it 'routes to /v1/auth to user_token#create' do
expect(:post => '/v1/users/auth.json').to route_to({"format"=>"json", "controller"=>"v1/sessions", "action"=>"create"})
end
Like above.
I had the same issue.
My controller was: api/v1/users
My route was defined this was:
Rails.application.routes.draw do
namespace :api, defaults: { format: :json } do
namespace :v1 do
resources :users
end
end
end
And I was defining my spec route this way:
RSpec.describe Api::V1::UsersController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(get: "api/v1/users").to route_to(controller: :api/v1/users, action: :index)
end
end
end
But each time I ran into the error:
The recognized options <{"format"=>"json", "action"=>"index", "controller"=>"api/v1/users"}> did not match <{"action"=>"index", "controller"=>"api/v1/users"}>
Here's how I fixed it:
I simply modified the spec route from this:
RSpec.describe Api::V1::UsersController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(get: "api/v1/users").to route_to(controller: :api/v1/users, action: :index)
end
end
end
to this:
RSpec.describe Api::V1::UsersController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(get: "api/v1/users").to route_to(format: :json, controller: "api/v1/users", action: "index")
end
end
end
That's all.
I hope this helps

Rspec and non-RestFul routes

I am doing homework but I have problem with non-RestFul routes.
My spec is:
require 'spec_helper'
describe MoviesController do
describe 'searching TMDb' do
before :each do
#fake_results = [mock('Movie'), mock('Movie')]
end
it 'should call the model method that performs TMDb search' do
Movie.should_receive(:find_in_tmdb).with('Star Wars').
and_return(#fake_results)
get :search_similar_movies, { :search_terms => 'Star Wars' }
end
end
end
In config/routes.rb I have:
resources :movies
'movies/search_similar_movies/:search_terms'
But when I run autotest it gives me error that begins with:
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.1.0/lib/action_dispatch/routing/mapper.rb:181:in `default_controller_and_action': missing :action (ArgumentError)
It's obvious that something is wrong is config/routes.rb. How to solve this?
Your route should be something like
resources :movies do
get 'search_similar_movies', :on => :collection
end
or
match 'movies/search_similar_movies/:search_terms' => 'movies#search_similar_movies', :via => :get

How to deal with RoutingError while testing with Rspec?

I created a controller called PolicyController and nested its route like this:
scope "/your"
resources :shops do
resources :policies
end
end
Now when I'm trying to test this controller I keep getting this error:
1) PoliciesController POST 'create' should be successful
Failure/Error: post 'create'
ActionController::RoutingError:
No route matches {:controller=>"policies", :action=>"create"}
# ./spec/controllers/policies_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
Not sure how to set it right. Would appreciate the help.
Edit: Forgot my specs:
describe PoliciesController do
describe "POST 'create'" do
it "should be successful" do
post 'create'
response.should be_success
end
end
Do you think this will work?
post :create, :shop_id => 1
Definitely want to create a new shop in a before block.

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

Resources