Rspec and non-RestFul routes - ruby-on-rails

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

Related

testing all matched routes (*path) in rails in Rspec

I have a rails application and I am trying to test the routes file.
How can one test this route in the routes.rb in rspec(minitest would be great as well):
'*unmatched_route', :to => 'application#raise_not_found!', :via => :all
According to Routing specs in RSpec you should be able to write something like this:
# in spec/routing/not_found.rb
describe 'not found' do
it 'routes to application#raise_not_found' do
expect(:get => "/an-unknown-route").to route_to(
:controller => "application",
:action => "raise_not_found!"
)
end
end

controller spec failing with ActionController::RoutingError with get method

I am building a Rails 3 application, and I am a little confused with a controller spec. here what I have in the application:
routes.rb
get "/:user_name/library", :to => 'users#library', :as => :user_library
users_controller.rb
class UsersController < ApplicationController
def library
end
end
users_controller_spec.rb
describe "UsersController" do
describe "#library" do
let(:user){FactoryGirl.create(:user)}
it "renders the users/library.html.erb view" do
get :library, :parameters => {:user_name => user.user_name}
end
end
end
this example dose not run and it shows the following error
Failure/Error: get :library,:parameters => {:user_name => user.user_name}
ActionController::RoutingError:
No route matches {:parameters=>{:user_name=>"UserName"}, :controller=>"users", :action=>"library"}
Get rid of the :parameters key in your get:
get :library, :user_name => user.user_name

Rspec "No route matches" error

I am attempting to test my sessions_controller in Rails 3 app with rspec, but keep coming across this error when I run rspec on my sessions_controller_spec.rb:
ActionController::RoutingError:
No route matches {:controller=>"sessions", :action=>"create"}
Here are all the relevant files:
routes.rb
match 'event' => 'event#create', via: [:post]
match 'event/agenda' => 'event#agenda', via: [:get]
match 'testLogin' => 'application#test_login', via: [:get]
post 'session' => 'session#create'
sessions_controller.rb
class SessionsController < ApplicationController
def create
#MY CODE HERE
end
end
sessions_controller_spec.rb
require 'spec_helper'
describe SessionsController, :type => :controller do
describe "POST #create" do
context "invalid params" do
it "returns a response with status failed if all required parameters are not passed in" do
post "create"
response.body.status.should eq("failed")
end
end
end
end
If there's any other info I can provide to help let me know. Thanks a lot!
post 'session' => 'session#create'
Your route definition is looking for a SessionController, but you have defined a SessionsController. Fix your route.
post 'session' => 'sessions#create'

Route works in browser, fails in Rspec

This question has probably been asked a dozen times on Stack Overflow (e.g. (1), (2), (3), (4), (5)) but every time the answer seems to be different and none of them have helped me. I'm working on a Rails Engine and I'm finding that Rspec2 gets route errors, but I can reach the routes in the browser. Here's the situation:
In the engine's routes.rb:
resources :mw_interactives, :controller => 'mw_interactives', :constraints => { :id => /\d+/ }, :except => :show
# This is so we can build the InteractiveItem at the same time as the Interactive
resources :pages, :controller => 'interactive_pages', :constraints => { :id => /\d+/ }, :only => [:show] do
resources :mw_interactives, :controller => 'mw_interactives', :constraints => { :id => /\d+/ }, :except => :show
end
Excerpted output of rake routes:
new_mw_interactive GET /mw_interactives/new(.:format) lightweight/mw_interactives#new {:id=>/\d+/}
...
new_page_mw_interactive GET /pages/:page_id/mw_interactives/new(.:format) lightweight/mw_interactives#new {:id=>/\d+/, :page_id=>/\d+/}
And my test, from one of the controller specs (describe Lightweight::MwInteractivesController do):
it 'shows a form for a new interactive' do
get :new
end
...which gets this result:
Failure/Error: get :new
ActionController::RoutingError:
No route matches {:controller=>"lightweight/mw_interactives", :action=>"new"}
...and yet when I go to that route in the browser, it works exactly as intended.
What am I missing here?
ETA: To clarify a point Andreas raises: this is a Rails Engine, so rspec runs in a dummy application which includes the engine's routes in a namespace:
mount Lightweight::Engine => "/lightweight"
...so the routes shown in rake routes are prefaced with /lightweight/. That's why the route shown in the Rspec error doesn't seem to match what's in rake routes. But it does make the debugging an extra step wonkier.
ETA2: Answering Ryan Clark's comment, this is the action I'm testing:
module Lightweight
class MwInteractivesController < ApplicationController
def new
create
end
...and that's it.
I found a workaround for this. Right at the top of the spec, I added this code:
render_views
before do
# work around bug in routing testing
#routes = Lightweight::Engine.routes
end
...and now the spec runs without the routing error. But I don't know why this works, so if someone can post an answer which explains it, I'll accept that.
I think the might be something wrong higher up in you specs
how did the "lightweight" get into this line :controller=>"lightweight/mw_interactives"
the route says
new_mw_interactive GET /mw_interactives/new(.:format)
not
new_mw_interactive GET /lightweight/mw_interactives/new(.:format)
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
then add a file spec/controllers/home_controller_spec.rb
require 'spec_helper'
describe HomeController do
context "GET index" do
before(:each) do
get :index
end
it {should respond_with :success }
it {should render_template(:index) }
end
end

Rails routing: override the action name in a resource/member block

I know how to do this with match but I really want to do it in the resource block. Here's what I have (simplified):
resources :stories do
member do
'put' collaborator
'delete' collaborator
end
end
I am looking for a way to allow the same action name in the URL but have different entry points in the controller. At the moment I've got in my controller:
# PUT /stories/1/collaborator.json
def add_collaborator
...
end
# DELETE /stories/1/collaborator.json
def remove_collaborator
...
end
So I tried this:
resources :stories do
member do
'put' collaborator, :action => 'add_collaborator'
'delete' collaborator, :action => 'remove_collaborator'
end
end
but this didn't seem to work when I wrote an rspec unit test:
describe "PUT /stories/1/collaborator.json" do
it "adds a collaborator to the story" do
story = FactoryGirl.create :story
collaborator = FactoryGirl.create :user
xhr :put, :collaborator, :id => story.id, :collaborator_id => collaborator.id
# shoulds go here...
end
end
I get this error:
Finished in 0.23594 seconds
5 examples, 1 failure, 1 pending
Failed examples:
rspec ./spec/controllers/stories_controller_spec.rb:78 # StoriesController PUT
/stories/1/collaborator.json adds a collaborator to the story
I'm assuming this error is because the way I'm trying to define my routes is incorrect...any suggestions?
Is the following better?
resources :stories do
member do
put 'collaborator' => 'controller_name#add_collaborator'
delete 'collaborator' => 'controller_name#remove_collaborator'
end
end
You should also check your routes by launching in a terminal:
$ rake routes > routes.txt
And opening the generated routes.txt file to see what routes are generated from your routes.rb file.

Resources