I was running tests without a problem on a cloud9 console. I made what I thought was a basically inconsequential change in my code to fix a testing fail, and got this error message:
rake aborted!
NameError: undefined local variable or method `migrateRails' for main:Object
The change I made was just to add a function to a controller, nothing to do with the test gem. I reinstalled the bundle and ran the test again. Same error.
I undid the change in the controller. Same error.
Thanks.
I think I find my error: when I wrote the test I didn't put "end" at the end of the test.
I did:
test "should get contact" do
get :contact
assert_response :success
assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
and I should have done:
test "should get contact" do
get :contact
assert_response :success
assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
end
Related
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
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>.
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.
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.
For some reason, my integration tests that are testing my login controller suddenly won't work. Just upgraded to 2.3.9 and been having weird problems. Everything was perfectly fine in 2.3.5 and even 2.3.8! Here's some code that no longer passes:
def login_user
visit login_url
assert_response :success
assert_template "new"
post session_url, :email => "abc#123.com", :password => "secret"
assert_response :redirect
follow_redirect!
assert_response :success
assert_template "sessions/organizations"
post organizations_session_url, :id => organizations(:ace).id
assert_response :redirect
follow_redirect!
assert_response :success
assert_template "mission_control/index"
end
When a user logs in their id gets saved to session[:user_id]. When I run the test above it fails after the first follow_redirect!. It appears that session[:user_id] is getting cleared and then asking the user to log in again when they just did. What could be causing my sessions to get cleared? I haven't touched anything. Rails 2.3.5 was fine. Is there a bug in Rails?
Thanks
(Btw, the site runs fine. It's just the integration tests that appear to be messing with my sessions.)
Well I figured it out. 2.3.10 had the same problem as well.
Instead of using the Rails 2.3.10 gem, I downloaded the latest stable code from their 2.3 branch on github and froze to that. I guess the Rails gems are outdated.
Thanks