Rails root test - ruby-on-rails

I am a beginner and am trying to test whether the following code maps to the "home page":
Rails.application.routes.draw do
root 'static_pages#home'
end
what should I replace the first and second "FILL_IN" with in the block below?
test "should get root" do
get FILL_IN
assert_response FILL_IN
end
Would appreciate your help!

I assume you are following the "Ruby on Rails Tutorial" by Micheal Hartl?
Specifically this step: Listing 3.42: A test for the root route
Although not explicitly disclosed in the tutorial (correct me if im wrong)...
I believe the solution below will pass your test. I tried it myself to double check.
test "should get root" do
get '/'
assert_response :success
end
Additional reading can be found here: Rails Guide - Integration Tests
Similar Posts: By filling in the code marked FILL_IN in Listing 3.42, write a test for the root route
Thanks #AlterLagos

Related

If I want to change the route in a test controllers file, do I have to edit any other files?

I want to change the route in this file from users_new_url to signup_path:
require 'test_helper'
class UsersControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
get users_new_url
assert_response :success
end
end
I have tried simply replacing it,
get signup_path
but when I run rails test, it always says that signup_path is an "undefined local variable or method". Do I need to edit other files?
If it helps, the following code is from test/controllers/users_controller_test.rb.
Update: I updated my routes.rb file with get 'signup', to: 'users#new'.
Yes, you need to modify your routes.rb and setup a named route signup in order for it to be available in your application, including tests. You could review Rails Routing from the Outside In to understand how the named route helpers work.

Ruby on Rails difference between get :home and static_pages_home_url

I am reading Ruby on Rails Tutorial (Ch 3.3.2) by Michael Hartl.
I am currently making a failing test(RED) However, I just noticed that his static_pages_controller_test.rb has
test "should get home" do
get :home
assert_response :success
end
and mine has although I followed everything he did.
test "should get home" do
get static_pages_home_url
assert_response :success
end
It seems like get :home is the same as get static_pages_home_url.
Is there any difference?
It does the same in this situation, but not in all situations.
since get :home is inside of your static_pages_controller_test.rb it looks in that controller automatically and finds the home method.
get static_pages_home_url is more dynamical. It looks for the home url inside of your static_pages_controller.
So you can call get_static_pages_home_url in tests on different locations but you can't with get :home.

functional test to confirm custom route with devise

I have a custom route for devise:
devise_scope :user do
get '/login' => "devise/sessions#new", :as => :new_user_session
get '/logout' => 'devise/sessions#destroy',
...
I want to make sure that when the request /login is called, that it gets correctly routed to devise and that the response is successful.
How do you test for custom routing and a successful request?
If you're using the built-in TestUnit to test, check out the Rails Guide on testing controllers.
If you're using Rspec, check out the Rspec Github Page for information on testing controllers.
It depends on you testing suite, personally I use RSpec + Capybara for my Rails projects.
If you don't have a testing suite yet I highly recommend this guide.
http://everydayrails.com/2012/03/12/testing-series-intro.html
In this particular instance, I'd say you want two tests. One for the routing and one for a successful request. Using rspec, this will look something like:
#spec/routing/devise_routing_spec.rb
require 'spec_helper'
describe "Devise Routes" do
it "should route the login correctly" do
{:get => "login"}.should route_to(controller: "devise/sessions", action: "new")
end
end
and
#spec/controllers/devise/session_controller_spec.rb
require 'spec_helper'
describe Devise::Sessions do
it "should be successful with a login request" do
get "new"
response.should be_success
end
end

Method get of ActionController::TestCase ignores routes.rb?

I'm running into issue which seems to indicate that ActionController::TestCase.get() method ignores what I have in routes.rb.
Rails version is 3.0.10.
I have the following RSpec2 test of my XmlRpcController#index action:
it "should get nothing in response to GET request" do
get :index
response.response_code.should == 400 #bad_request
end
And the only line related to this route in routes.rb is:
post 'rpc', :to => "xml_rpc#index"
'rake routes' also shows only this route defined.
As a result when I run this test that action actually DOES get executed! I judge this by putting a simple puts inside it) and also a log contains:
Processing by XmlRpcController#index as HTML
Also if I go to 'localhost:3000/rpc' in browser - it says no route found: just like it should. But tests have other behavior and this puzzles me...
Can anybody hint my why does this happen? I'm only starting learning about RoR :)
Earlier it seemed to me that these 'get/post' methods of TestCase do respect routes.rb...
Am I missing something obvious? :)
It seems that 'get :index' method is ignoring the routes.rb indeed.
The real solution for me was to use be_routable rspec matcher written for this particular purpose:
describe "GET 'contact'" do
it "should be successful" do
{ :get => '/rpc' }.should_not be_routable
end
end
Figured this out thanks to some user from Ruby-Forum. More info here.

Rails 3 and Rspec 2 namespaced controller problems

I;m trying to write a spec that tests a controller that is namespaced, let's call it Admin::FooController. My spec is called admin_foo_controller_spec.rb. In the spec, I'm trying to write a very basic spec to see if it can retrieve an action of a controller.
describe "GET 'index'" do
it "should be successful" do
get 'index'
response.should be_success
end
end
But instead I get an error:
Failure/Error: get 'index'
No route matches {:controller=>"admin/foo"}
For another action, I have basically the same test, and I get that the response is not successful. In the actual webapp, I can access these urls fine. One thing I should mention is that this isn't a RESTful resource (it makes no sense as one) so it's really just a group of related administrative actions, so in my routes.rb it's basically like
namespace :admin do
scope "foo" do
match "action1" => "foo#action1"
match "action2" => "foo#action2"
match "/index" => "foo#settings"
match "settings" => "foo#settings"
end
end
Any ideas what I'm doing wrong? It seems like namespacing is asking for trouble with rails, especially for a novice like me. Thanks!
In your route your have no index action in you Foo controller try to get 'settings'
describe "GET 'index'" do
it "should be successful" do
get 'settings'
response.should be_success
end
end
In a controller spec you need define the action of your controller not the real route. You try the routes in a integration test.

Resources