Ruby on Rails difference between get :home and static_pages_home_url - ruby-on-rails

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.

Related

Rails root test

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

Getting a rails test to pass

Following a rails tutorial but when off on a slight tangent and now cant get a test to pass.
Below is my routes file and my test file. The home test is failing and this line , to: "static_pages#home", as: "home" is the line that makes it appear. i.e remove this and it'll pass. I was wondering why someone could explain why this is failing and how to alter the test to make it pass but keeping this line in?
My routes file:
Rails.application.routes.draw do
resources :static_pages
get 'static_pages/help'
get 'static_pages/test'
get 'static_pages/home', to: "static_pages#home", as: "home"
root 'application#hello'
end
My test file:
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
test "should get home" do
get static_pages_home_url
assert_response :success
end
test "should get help" do
get static_pages_help_url
assert_response :success
end
test "should get test" do
get static_pages_test_url
assert_response :success
end
end
static_pages_home_url is the default helper method for that route with controller static_pages and action home but you set the name as home so your test should be
test "should get home" do
get home_url
assert_response :success
end

Rails Tutorial static pages routes test red (5.28)

Doing Michael Hartl's Rails Tutorial, but stumped on Listing 5.28 (getting RED test instead of GREEN), changing the test to match the new routes.
ERRORS for all pages (/, /about, /contact, /help):
ActionController::UrlGenerationError: No route matches {:action=>"/*", :controller=>"static_pages"}
routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
end
tests/controllers/static_pages_controller_test.rb
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get root_path
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
test "should get help" do
get help_path
assert_response :success
assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
end
test "should get about" do
get about_path
assert_response :success
assert_select "title", "About | Ruby on Rails Tutorial Sample App"
end
test "should get contact" do
get contact_path
assert_response :success
assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
end
end
static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
def contact
end
end
Let me know if you need to see any other code! Have tried adding as: '*' after each get route, but to no avail.
Not sure if it is a ruby/rails version issue, but I am using Rails 4.2.2 and Ruby 2.3.0 on an IDE, but "rails test" (as Hartl instructs to use) won't work (kicks back "test Command not found"). Not sure if that's a hint to a bigger problem or unrelated. Thanks in advance!
EDIT: Links using these paths (like below) are rendering correctly, it is just failing the tests.
<%= link_to "Home", root_path %>
<%= link_to "Help", help_path %>
Your test/controllers/static_pages_controller_test.rb have problem. I would suggest to replace it with below contents.
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
test "should get home" do
get root_path
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
test "should get help" do
get help_path
assert_response :success
assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
end
test "should get about" do
get about_path
assert_response :success
assert_select "title", "About | Ruby on Rails Tutorial Sample App"
end
test "should get contact" do
get contact_path
assert_response :success
assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
end
end
Change the above file as I have suggested and run $ rails test you can see green test. I also suggest you to upgrade your app to rails 5.0.0 as Michael Hartl's have been upgraded his rails tutorials to rails 5.0.0 in future you have more things to learn and if you upgrade it, your journey of learning would be more error free and pleasant.

Why doesn't this Rails Rspec Test Work?

I create all the routes of my program manually and so do with my rspec tests of course. Generally, my routes and tests work fine, but i have a problem with the test for my characters controller. The route is :
scope :path => '/characters', :controller => :characters do
get '/' => :show, :as => 'user_character'
end
The /characters works fine when tested with my browser. Everything seems fine. But, the test :
require 'spec_helper'
require 'devise/test_helpers'
describe CharactersController do
login_user
describe "when it GETS 'show'" do
it "should render the template and be successful" do
get :show
response.should render_template(:show)
response.should be_success
end
end
end
Fails with the error :
1) CharactersController when it GETS 'show' should render the template and be successful
Failure/Error: get :show
ActionController::RoutingError:
No route matches {:controller=>"characters", :action=>"show"}
# ./spec/controllers/characters_controller_spec.rb:9
All my controllers have similar tests that work fine. Why does this not work ?
IMPORTANT EDIT :
Just saw that if i turn Spork off, the test passes ! Why is this happening ? Does Spork need to be restarted every time a new test is added ?
You have to restart spork when changing routes.
Or put this in your spec_helper.rb:
Spork.each_run do
ApplicationName::Application.reload_routes!
end
See also "Speedy Test Iterations for Rails 3 with Spork and Guard"

How to test an RSS feed, with cucumber or rspec

Anyone have any tips on how to test an rss feed with cucumber (preference) or rspec?
Note, I am currently working on a Rails 3 application with a blog which I expose as an rss feed.
I would like to set up a test to ensure that it remains well formatted and consumable.
Thanks!
Jonathan
To test an RSS feed with RSpec you can use :format => "rss" in your controller tests, i.e. you can simple use something like this:
describe "GET RSS feed" do
it "returns an RSS feed" do
get :index, :format => "rss"
response.should be_success
response.should render_template("posts/index")
response.content_type.should eq("application/rss+xml")
end
end
Using Test::Unit, you would write
def test_get_rss_feed
get :index, :format => "rss"
assert_response :success
assert_template "posts/index"
assert_equal 'application/rss+xml', #response.content_type
end
You could find some ideas by looking at the specs for an existing RSS gem, such as feedzirra.

Resources