Rails tutorial Testing failure - ruby-on-rails

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.

Related

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.

Hartl Rails Tutorial - Chapter 3

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>.

Ruby giving me similar four failures that other people have had

I keep getting a few errors that I have seen from other people on here. Here is the error message:
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
<About | 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]:
<Contact | Ruby on Rails Tutorial Sample App> expected but was
<Contact | Ruby on Rails Tutorial Sample App.>.
Expected 0 to be >= 1.
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
<Help | Ruby on Rails Tutorial Sample App.>.
Expected 0 to be >= 1.
4) Failure:
StaticPagesControllerTest#test_should_get_home [C:/Sites/sample_app/test/controllers/static_pages_controller_test.rb:7]:
<Home | Ruby on Rails Tutorial Sample App> expected but was
<Ruby on Rails Tutorial Sample App.>.
Expected 0 to be >= 1.
Here is also code for the test file:
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 :contact
assert_response :success
assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
end
end
How do I fix this problem on my application? Do I have to write another part of code for the program?
You should try adding a . (period/full stop) after Sample App in your tests, or else removing the . from your view.
Like this:
test "should get home" do
get :home
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App."
end
the message Expected 0 to be >= 1 simply means the test was counting how many times it could find the content (without the .) on the page and it counted 0 but it expected the count to be at least 1 since that is what you assert in you test.

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.

RoR 2.3.11 and Rspec 1.2.9 ... adding tests to existing project: tests exist however they're ignored

Hey all this is where I'm at:
$ spec spec/
Finished in 0.002031 seconds
0 examples, 0 failures
HOWEVER. I've got my first few tests I've written and placed them in /spec/controllers/citations_controller_spec.rb
and added a puts in the above spec to verify that its actually being executed on the use of:
spec spec/
here are the contents of citations_controller_spec.rb ( it has existing tests ) :
require 'spec_helper'
describe CitationsController do
describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success
end
it "should have the right title" do
get 'home'
response.should have_tag("title",
"Ruby on Rails Tutorial Sample App | Home")
end
end
describe "GET 'contact'" do
it "should be successful" do
get 'contact'
response.should be_success
end
it "should have the right title" do
get 'contact'
response.should have_tag("title",
"Ruby on Rails Tutorial Sample App | Contact")
end
end
describe "GET 'about'" do
it "should be successful" do
get 'about'
response.should be_success
end
it "should have the right title" do
get 'about'
response.should have_tag("title",
"Ruby on Rails Tutorial Sample App | About")
end
end
end
But as shown in the first code bit ...
0 examples, 0 failures
.. so anyone happen to know whats going on here?
Try
spec spec/controllers/citations_controller_spec.rb
to run one test file. Or
rake test
to run all tests. Do you see any test dots or examples now?

Resources