How to test for routing error in rspec? - ruby-on-rails

I'm simplifying a resource by removing the show action since its not needed. only listing, creating and editing are needed. I still have my SHOW test in my rspecs and its now failing (obviously since I've added an :except => [:show] to my routes file.
This is what I'm getting as a failure:
1) CampaignsController GET show assigns the requested campaign as #campaign
Failure/Error: get :show, {:id => campaign.to_param}, valid_session
ActionController::RoutingError:
No route matches {:id=>"458", :controller=>"campaigns", :action=>"show"}
# ./spec/controllers/campaigns_controller_spec.rb:49:in `block (3 levels) in <top (required)>'
2) CampaignsController routing routes to #show
Failure/Error: expect(:get => "/campaigns/1").to route_to("campaigns#show", :id => "1")
No route matches "/campaigns/1"
# ./spec/routing/campaigns_routing_spec.rb:15:in `block (3 levels) in <top (required)>'
How can I make these tests pass so that I'm expecting a routing error?

You could do
expect{ get :show }.to raise_error(ActionController::RoutingError)
See https://www.relishapp.com/rspec/rspec-expectations/v/2-0/docs/matchers/expect-error
You might also consider routing specs: https://www.relishapp.com/rspec/rspec-rails/v/2-4/docs/routing-specs

For anyone interested the Minitest equivalent would be something like:
def CampaignsControllerTest < ActionDispatch::IntegrationTest
assert_raises ActionController::RoutingError do
get "/campaigns/1"
end
end

Related

Rails routes, rspec

I am attempting to create a route to a controller method, in order that it pass an rpsec condition. For some reason that escapes me, RSPEC will not accept the route.
Here is the rspec:
describe BooksController do
describe 'searching AMZN' do
it 'should call the model method that performs AMZN search' do
post :search_tmdb, {:search_terms => 'hardware'}
end
My routes file reads:
Rottenpotatoes::Application.routes.draw do
resources :books
post '/books/search_amzn'
# map '/' to be a redirect to '/books'
root :to => redirect('/books')
end
The controller action in books_controller.rb:
def search_amzn
#books = Books.find_in_amzn(params[:search_terms])
end
I am clearly making an error in my route for I can't get away from this error message:
1) MoviesController searching AMZN should call the model method that performs AMZN search
Failure/Error: post :search_amzn, {:search_terms => 'hardware'}
ActionController::RoutingError:
No route matches {:search_terms=>"hardware", :controller=>"books", :action=>"search_amzn"}
# ./spec/controllers/books_controller_spec.rb:9:in `block (3 levels) in <top (required)>'
Here are links to the complete files:
Routes.rb: http://pastebin.com/yKBeLLnY
Spec: http://pastebin.com/cU3nRSvE
Change this:
post :search_tmdb, {:search_terms => 'hardware'}
to:
get :search_tmdb, {:search_terms => 'hardware'}
Also, in your routes file, change this:
post '/books/search_amzn'
to:
post '/books/search_amzn', as: :search_tmdb

RoR and Rspec setting up a test using a while loop

Ok so I want to create a test for checking that all pages have a certain title. However I thought it would be nice if I could include the page titles in an array so that I wouldn't have to duplicate the block for each page. And it would allow me to test additional pages by just modifying the pages array.
The issue I am having is that the page variable is not being interpolated in the test. So is this a syntax error or does Rspec not allow interpolation within the it should do... block?
describe "LayoutLinks" do
page = ["home", "contact", "about", "help"]
i = page.count
x = 0
while x < i
it "should have a #{page[x]} page" do
get "#{page[x]}"
response.should have_selector("title", :content => "#{page[x]}")
end
x += 1
end
end
Test shows the following failures:
1) PagesController LayoutLinks should have a help page
Failure/Error: get "#{page[x]}"
ActionController::RoutingError:
No route matches {:controller=>"pages", :action=>""}
# ./spec/controllers/layout_links_spec.rb:14:in `block (3 levels) in <top (required)>'
2) PagesController LayoutLinks should have a contact page
Failure/Error: get "#{page[x]}"
ActionController::RoutingError:
No route matches {:controller=>"pages", :action=>""}
# ./spec/controllers/layout_links_spec.rb:14:in `block (3 levels) in <top (required)>'
3) PagesController LayoutLinks should have a about page
Failure/Error: get "#{page[x]}"
ActionController::RoutingError:
No route matches {:controller=>"pages", :action=>""}
# ./spec/controllers/layout_links_spec.rb:14:in `block (3 levels) in <top (required)>'
4) PagesController LayoutLinks should have a home page
Failure/Error: get "#{page[x]}"
ActionController::RoutingError:
No route matches {:controller=>"pages", :action=>""}
Failure error here is obvious. It shouldn't say get "#{page[x]}" but rather it should be get home and get about, etc...
How do I remedy? Thanks for the help. Much appreciated :)
Try the following:
describe "LayoutLinks" do
%w(home contact about help).each do |page|
it "should have a #{page} page" do
get page
response.should have_selector("title", content: page)
end
end
end
%w creates an array of strings (spaces become commas, so you get ["home", "contact", etc])

rspec controller spec accessing route

I have a location resource and access via:
http://localhost:3000/locations/37/edit
In my spec, I have:
it "should allows us to edit" do
#u=User.find_by_email('jon#domain.com')
session[:user_id]=#u.id
get edit_location_path, {:id => '37'}
but get the following error:
Failures:
1) LocationsController should allows us to edit
Failure/Error: get edit_location_path, :id => '37'
ActionController::RoutingError:
No route matches {:action=>"edit", :controller=>"locations"}
# ./spec/controllers/locations_controller_spec.rb:12:in `block (2 levels) in <top (required)>'
How would I specify the link to this resource?
thx
Assuming it's a controller spec describing LocationsController, you can access it with get :edit, :id => 37.
Just do get edit_location_path(37) ??

RoutingError in Rspec

I keep running across this error for a GET request to show action.
1) ShopController GET 'show' should be successful
Failure/Error: get 'show', :id=>#shop.user.nickname
ActionController::RoutingError:
No route matches {:id=>"picardo", :controller=>"shop", :action=>"show"}
# ./spec/controllers/shop_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
My routes look like this.
#routes.rb
resources :shop, :only=>[:show]
This is the controller spec:
#shop_controller_spec.rb
before(:each) do
#shop = Fabricate(:shop)
end
describe "GET 'show'" do
it "should be successful" do
get 'show', :id=>#shop.user.nickname
response.should be_success
end
end
And teh controller:
def show
#user = User.find(:first,:conditions=>{:nickname=>params[:id]})
#shop = #user.shop
end
You have to follow some rails rules.
If you have a singular resource you have to write resource :shop and if you have the plural resources you have to write resources :shops. You can run rake routes command to see the difference.
You wrote resources :shop and request sends to ShopController that is not exist in your application because I am sure that controller named ShopsController. Because the another rails rule is to name controllers in plural form and models in singular.
Or just write a controller name in routes: resources :shop, :controller => 'shops'

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.

Resources