Hartl Rails Tutorial - Chapter 3 - ruby-on-rails

I'm completing the exercise to add a Contact page, but the testing fails on the page title.
Here is my testing file:
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
def setup
#base_title = "Ruby on Rails Tutorial Sample App"
end
test "should get root" do
get root_url
assert_response :success
end
test "should get home" do
get static_pages_home_url
assert_response :success
assert_select "title", "Home | #{#base_title}"
end
test "should get help" do
get static_pages_help_url
assert_response :success
assert_select "title", "Help | #{#base_title}"
end
test "should get about" do
get static_pages_about_url
assert_response :success
assert_select "title", "About | #{#base_title}"
end
test "should get contact" do
get static_pages_about_url
assert_response :success
assert_select "title", "Contact | #{#base_title}"
end
end
Here is the contact.html.erb file:
<% provide(:title, "Contact") %>
<h1>Contact</h1>
<p>
Contact the Ruby on Rails Tutorial about the sample app at the
contact page.
</p>
I've also completed the following:
Added the appropriate route
Added the appropriate action
However I get this error message:
test_should_get_contact#StaticPagesControllerTest (0.45s)
<Contact | Ruby on Rails Tutorial Sample App> expected but was
<About | Ruby on Rails Tutorial Sample App>..
Expected 0 to be >= 1.
test/controllers/static_pages_controller_test.rb:35:in `block in <class:StaticPagesControllerTest>'
Please also note that
The page displays correctly, with the expected page title (Contact not About)
I tested again using a completely new page, but had the same result with 'About' being returned in page title
Really not sure why it's returning this as I've followed Tutorial closely. I want to progress in Tutorial, but if I cannot resolve this basic testing issue, I'm not sure I'll get very far!

Please check your code on the second line of this code block.
test "should get contact" do
# get static_pages_about_url # This is wrong correct it to as below
get static_pages_contact_url
assert_response :success
assert_select "title", "Contact | #{#base_title}"
end
You have given a test case to check the contact page title on the about url which obviously will fail the test.
You should be testing for contact page title on the contact url like above.
Make the change and you should get going!
Also a word of motivation, just keep going even if things don't make sense right now cause later they will. Cheers :)

I think you might try to replace the line get static_pages_about_url (under test "should get contact" do) with:
get static_pages_contact_url
What happens is that your test is calling the wrong url (about, instead of contact), causing the error when checking the <title>.

Related

How to use the edit_path in Rails for testing

I am setting up automatic testing for my Rails application. I am working on my controller test and I am getting an unrecognized id error. I know the edit route works for the app right now because I can go to the actual link(/adjusters/3293/edit), but I cannot get my test to past.
def setup
#adjuster = Adjuster.new(adjuster_name: "TestLastName", address_1: "4511 W 200 S", id: 1)
end
test "should get edit" do
get edit_adjuster_path(#adjuster)
assert_response :success
end
When I rake my routes the edit route shows.
edit_adjuster GET /adjusters/:id/edit(.:format) adjusters#edit
Here is the error I am receiving
Couldn't find Adjuster with 'id'=1
My show route works though
test "should get show" do
get adjusters_path(#adjuster)
assert_response :success
end

Can someone identify the error in my code causing my test to fail?

I keep running this test and it continually fails, yet I cannot find an error. The failure message is:
[Failure:
StaticPagesControllerTest#test_should_get_contact [/Users/iThompkins/Documents/SiteDev/environment/sample_app/test/controlle
rs/static_pages_controller_test.rb:26]:
<Contact | Ruby on Rails Tutorial Sample App> expected but was
<Ruby on Rails Tutorial Sample App>..
Expected 0 to be >= 1.]
My test looks like:
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
test "should get home" do
get static_pages_home_url
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
test "should get help" do
get static_pages_help_url
assert_response :success
assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
end
test "should get about" do
get static_pages_about_url
assert_response :success
assert_select "title", "About | Ruby on Rails Tutorial Sample App"
end
test "should get contact" do
get static_pages_contact_url
assert_response :success
assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
end
end
My controller
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
def contact
end
end
And my View
<% provide(:title, 'Contact') %>
Contact
Contact the Ruby on Rails Tutorial about the sample app at the
contact page.
Have you got <%= yield(:title) %> in the title section of your view?
<title><%= yield(:title) %> | Ruby on Rails Tutorial Sample App</title>
From the Hartl book, you need to yield the title value to get it to display.

Rails tutorial Testing failure

I am following a Rails tutorial..
Here is my testing file
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
def setup
#base_title = "Ruby on Rails Tutorial Sample App"
end
test "should get home" do
get static_pages_home_url
assert_response :success
assert_select "title", "Home | #{#base_title}"
end
test "should get help" do
get static_pages_help_url
assert_response :success
assert_select "title", "Help | #{#base_title}"
end
test "should get about" do
get static_pages_about_url
assert_response :success
assert_select "title", "About | #{#base_title}"
end
end
This is the error message i get:
Failure:
StaticPagesControllerTest#test_should_get_home
[c:/Sites/workspace/sample_app/te
st/controllers/static_pages_controller_test.rb:12]:
<Home | Ruby on Rails Tutorial Sample App> expected but was
<Home>..
Expected 0 to be >= 1.
Do take a not that the error sometimes shows About/Help in place of Home.
BTW when i comment out lines 5 to 7 and replace #{#base_title} in each of the tests the rails test result is success then.
Really conflicted with what is going on here.

Why is this simple minitest failing?

I'm new to testing rails applications as I usually just do manual testing... but I'm trying to do it the right way this time.
Why is this basic test failing?
test "once you go to to app you are asked to sign in" do
get "/"
assert_redirected_to "/users/sign_in"
assert_select "title", "Home Sign-In"
end
The first assertion is successful but not the second. The title seems correct when I view source.
<title>Home Sign-In</title>
If you have redirect call in your controller method it is not acturally rendered. That's why you can't use assert_select.
You may try to divide your test case into two:
test "once you go to to app you are asked to sign in" do
get "/"
assert_redirected_to "/users/sign_in"
end
test "sign in page title is correct" do
get "/users/sign_in"
assert_select "title", "Home Sign-In"
end
#Pavel is right.
A simple way to check the response after get request is #response.body.
So in your case
test "once you go to to app you are asked to sign in" do
get "/"
assert_redirected_to "/users/sign_in"
byebug #print #response.body here. At this point #response.body will
# be "You are being redirected to /users/sign_in".
assert_select "title", "Home Sign-In"
end
So you can modify it as Pavel has suggested
test "sign in page title is correct" do
get "/users/sign_in"
assert_select "title", "Home Sign-In"
end

Ruby on Rails test "Expected response but returned 200

I am an experienced web developer and am starting to learn ruby on rails with the use of HTML and CSS tied into the code for rails. I have a test website and am trying to run it on ruby on rails. The test will display a website with a home, help, about, and contact page under one rails application on the web.
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get :home
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
test "should get help" do
get :help
assert_response :success
assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
end
test "should get about" do
get :about
assert_response :success
assert_select "title", "About | Ruby on Rails Tutorial Sample App"
end
test "should get contact" do
get :about
assert_response :success
assert_response "title, Contact | Ruby on Rails Tutorial Sample App"
end
end
However, when I try to run the test on the command prompt, it returns 3 failures on the command prompt. Each of the failures states that the program was expecting a format but received another format of the same type.
FFF.
Finished in 0.527144s, 7.5881 runs/s, 15.1761 assertions/s.
1) Failure:
StaticPagesControllerTest#test_should_get_about [C:/Sites/sample_app/test/controllers/static_pages_controller_test.rb:19]:
<About | Ruby on Rails Tutorial Sample App> expected but was
<Ruby on Rails Tutorial Sample App>.
Expected 0 to be >= 1.
2) Failure:
StaticPagesControllerTest#test_should_get_contact [C:/Sites/sample_app/test/controllers/static_pages_controller_test.rb:25]:
Expected response to be a <title, Contact | Ruby on Rails Tutorial Sample App>, but was <200>.
--- expected
+++ actual
## -1 +1 ##
-"title, Contact | Ruby on Rails Tutorial Sample App"
+200
3) Failure:
StaticPagesControllerTest#test_should_get_help [C:/Sites/sample_app/test/controllers/static_pages_controller_test.rb:13]:
<Help | Ruby on Rails Tutorial Sample App> expected but was
<Ruby on Rails Tutorial Sample App>.
Expected 0 to be >= 1.
How do I fix this within the program? Is there a way to make the program view the code the correct way? If I have to change it, what should I change it to?
OK so it means there is no HTML match please see this answer:
Failure: Expected 0 to be >= 1 on ruby on rails
Your test
test "should get contact" do
get :about
assert_response :success
assert_response "title, Contact | Ruby on Rails Tutorial Sample App"
end
should be
test "should get contact" do
get :contact
assert_response :success
assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
end
Other failures are because of title of tested page does not match the expected title in the test, i.e. there's some bug in your app or in the test.
1) Failure:
StaticPagesControllerTest#test_should_get_about [C:/Sites/sample_app/test/controllers/static_pages_controller_test.rb:19]:
<About | Ruby on Rails Tutorial Sample App> expected but was
<Ruby on Rails Tutorial Sample App>.
Expected 0 to be >= 1.

Resources