Ruby giving me similar four failures that other people have had - ruby-on-rails

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.

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.

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.

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

Hartl's railstutorial.org Chapter 5, exercise 2

I'm ploughing through Michael Hartl's Ruby on Rails Tutorial (https://www.railstutorial.org/book/filling_in_the_layout) right now and found an issue I can't google fix.
Upon creating this file: test/helpers/application_helper_test.rb, and running:
'rails test'
I'm getting this failure:
AIL["test_full_title_helper", ApplicationHelperTest, 0.7209667120041559]
test_full_title_helper#ApplicationHelperTest (0.72s)
--- expected
+++ actual
## -1 +1 ##
-"Help | Ruby on Rails Tutorial Sample App"
+"Ruby on Rails Tutorial Sample App"
test/helpers/application_helper_test.rb:6:in `block in <class:ApplicationHelperTest>'
Having browsed similar questions, albeit questions over a year old (and using older Rails code), I'm none the wiser as to how to rectify this failure.
I am clearly a Ruby noobie, so please be gentle :) any help would be greatly appreciated
static_pages_controller_test.rb:
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
and the offending file:
require 'test_helper'
class ApplicationHelperTest < ActionView::TestCase
test "full title helper" do
assert_equal full_title, "Ruby on Rails Tutorial Sample App"
assert_equal full_title("Help"), "Help | Ruby on Rails Tutorial Sample App"
end
end
and the site_layout_test.rb
require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
test "layout links" do
get root_path
assert_template 'static_pages/home'
assert_select "a[href=?]", root_path, count: 2
assert_select "a[href=?]", help_path
assert_select "a[href=?]", about_path
assert_select "a[href=?]", contact_path
get contact_path
assert_select "title", full_title("Contact")
end
end
and the code for the application_helper:
module ApplicationHelper
# Returns the full title on a per-page basis.
def full_title(page_title = '')
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
page_title + " | " + base_title
end
end
end
help.html.erb static_page:
<% provide(:title, "Help") %>
<h1>Help</h1>
<p>
Get help on the Ruby on Rails Tutorial at the
Rails Tutorial help section.
To get help on this sample app, see the
<a href="http://www.railstutorial.org/book"><em>Ruby on Rails Tutorial</em>
book</a>.
</p>
I'm working on the same book, and am at the same chapter as you! I see the following:
--- expected
+++ actual
## -1 +1 ##
-"Help | Ruby on Rails Tutorial Sample App"
+"Ruby on Rails Tutorial Sample App"
Rearranged:
--- expected
-"Help | Ruby on Rails Tutorial Sample App"
+++ actual
+"Ruby on Rails Tutorial Sample App"
The test expected "Help | Ruby on Rails Tutorial Sample App", but actually got "Ruby on Rails Tutorial Sample App".
If you look at the help page in a browser, what shows up in the title bar? The help.html.erb looks right to me.
I also have the following for my app/helpers/application_helper.rb, is a little different from yours:
module ApplicationHelper
# Returns the full title on a per-page basis
def full_title(page_title = '')
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
page_title + " | " + base_title
end
end
end
Here is my full_title method.
module ApplicationHelper
def full_title(page_title = "")
base_title = "Ruby on Rails Tutorial Sample App"
line = page_title.empty? ? "" : " | "
return "#{page_title}#{line}#{base_title}"
end
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