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
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>.
Background
I want to write the following test for my Ruby on Rails application.
Log in as user.
Visit edit user page.
Click link in nav to go to About Us page.
Actually visit the About Us page and confirm its the correct template.
The reason is that I found a bug where for the link About Us I have it be referential for '/welcome/about/' and so it goes to /users/welcome/about which doesn't exist.
My Code
My test code is as follows:
test 'welcome page from user page works' do
log_in_as(#user)
get edit_user_path(#user)
assert_select "a", { :text=> "About Us" }
click_on ("About Us")
assert_template 'welcome/about'
end
Error Message
ERROR["test_welcome_page_from_user_page_works", WelcomePageTest, 1.42307711095782]
test_welcome_page_from_user_page_works#WelcomePageTest (1.42s)
Capybara::ElementNotFound: Capybara::ElementNotFound: Unable to find link or button "About Us"
test/integration/welcome_page_test.rb:13:in `block in <class:WelcomePageTest>'
Finished in 1.42605s
1 tests, 1 assertions, 0 failures, 1 errors, 0 skips
Since there is 1 assertion this makes me think that the assertion the link exists pasts but somehow click_link can't find it?
EDIT This was my final code for the test:
test 'welcome page from user page works' do
log_in_as(#user)
visit edit_user_path(#user)
click_on ("About Us")
assert_template 'welcome/about'
end
Note I also changed the routes to be just hardcoded in:
get 'about' => 'welcome#about'
For capybara use the visit method instead of get
visit edit_user_path(#user)
Can an assert_select work after an assert_redirected_to? I have the following controller action and I'm getting a failure.
test "get switchboard" do
...
assert_redirected_to employees_url # success
assert_select 'div' # fail
end
This question is old, but I'll go ahead and answer it since I ran into a similar problem. assert_select can work after a redirect, but you must first tell the test to "follow" the redirect. So, in your case, you could do something like this:
test "get switchboard" do
...
assert_redirected_to employees_url # redirected, not "success" per se
follow_redirect! # this is what you need to do
assert_response :success # if you still want to...
assert_select 'div'
end
assert_select works just as its described in the docs.
I assume that you are trying to do the assert_select on some element on the page that you are redirecting to. The problem with this is that that redirected route is actually not getting rendered.
The first thing that I would do if I were you is toss a binding.pry right in front of that assert_select and take a look at the response.body which will reveal what is actually being rendered. My guess is that you will see something like :
<html><body>You are being redirected.</body></html>"
rather than actual page that you is going to be redirected to.
The answer is stated above in the comments:
add follow_redirect! after assert_redirected_to and your assert_select statements will pass
My controller action does the following:
Saves a newly POSTed instance of a user
Sends an email to the new user
Redirects to the user's homepage
I'm doing some quick and dirty view tests, just in the controller test... not robust enough for me to want to separate out into view test.
The problem is that I can test #1 and #2, but not #3. In other words, here is my test code:
describe "create - with valid, new data" do
it "creates a new signup" do
expect{
post :create, signup: FactoryGirl.attributes_for(:signup)
}.to change(Signup,:count).by(1)
end
it "redirect to form page" do
post :create, signup: FactoryGirl.attributes_for(:signup)
response.should render_template "subscribe/notification_email"
end
it "redirect to form page" do
post :create, signup: FactoryGirl.attributes_for(:signup)
response.should render_template "homepage"
end
end
The first two tests are passing, the last is failing. Is there a way to tell the controller test to go to the next thing that is rendered? Or how else can I test this?