I am trying to write an integration test on rails which is supposed to visit my various pages but I can't get past the login.
require 'test_helper'
class UserSimulationTest < ActionDispatch::IntegrationTest
test "login site" do
# login via https
https!
get "users/sign_in"
assert_response :success
post_via_redirect "users/sign_in", username: users(:User_1).email
assert_equal "/users/sign_in", path
https?
assert_response :success
end
test "go to voting" do
https!
get "voting"
post_via_redirect "users/sign_in", username: users(:User_1).email
post_via_redirect "voting"
assert_equal "/voting", path
assert_response :success
end
end
Then i get this error because it redirects me to the login again.
Minitest::Assertion: Expected: "/voting"
Actual: "/users/sign_in"
test/integration/user_simulation_test.rb:23:in `block in <class:UserSimulationTest>'
Finished in 0.84105s
2 tests, 4 assertions, 1 failures, 0 errors, 0 skips
Process finished with exit code 0
I managed to solve my problem by passing the correct username and password parameters from my fixtures. I am using devise so i created a login method in test_helper.rb
def login(user)
post_via_redirect user_session_path, 'user[email]' => user.email, 'user[password]' => user.encrypted_password
end
Related
I'm using Rails 6 to work through the Hartl Tutorial, using mostly the online version. I'm in Chapter 10 (which doesn't correspond to the book version chapter numbering)...
https://www.railstutorial.org/book/updating_and_deleting_users
In Section 10.2.2, after Listing 10.25, the Rails Test should be green, but I get these 2 FAIL messages:
FAIL["test_should_redirect_edit_when_not_logged_in", #, 11.729448000027332]
test_should_redirect_edit_when_not_logged_in#UsersControllerTest (11.73s)
Expected true to be nil or false
test/controllers/users_controller_test.rb:18:in `block in '
FAIL["test_should_redirect_update_when_not_logged_in", #, 1.2034909999929368]
test_should_redirect_update_when_not_logged_in#UsersControllerTest (1.20s)
Expected true to be nil or false
test/controllers/users_controller_test.rb:26:in `block in '
The related code is this:
test "should redirect edit when not logged in" do
log_in_as(#other_user)
get edit_user_path(#user)
assert_not flash.empty?
assert_redirected_to login_url
end
test "should redirect update when not logged in" do
log_in_as(#other_user)
patch user_path(#user), params: { user: { name: #user.name,
email: #user.email } }
assert_not flash.empty?
assert_redirected_to login_url
end
It's the log_in_as(#other_user) that appears to be the problem, but it could be elsewhere. I can't see why this section has a problem when the similar one in users_edit_test.rb does not.
I'm using Rails 5 and minutest and trying to write a test for a controller. The test is
# issues_controller_test.rb
class IssuesControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
test "logged in should get issues page" do
sign_in users(:one)
test_stop_id = 1
test_line_id = 1
get new_issue, :stop_id => test_stop_id, :line_id => test_line_id
assert_equal test_stop_id, #issue.stop_id
assert_equal test_line_id, #issue.line_id
assert_response :success
end
end
The method in question attempts to access this page, defined in my rails routes ...
new_issue GET /issues/new(.:format) issues#new
Yet when I run the test, I get this error
# Running:
.E
Error:
IssuesControllerTest#test_logged_in_should_get_issues_page:
NameError: undefined local variable or method `new_issue' for #<IssuesControllerTest:0x007f816759b330>
test/controllers/issues_controller_test.rb:10:in `block in <class:IssuesControllerTest>'
bin/rails test test/controllers/issues_controller_test.rb:6
Finished in 0.103956s, 19.2389 runs/s, 9.6195 assertions/s.
Why is the test framework unable to find a method defined in my routes file?
new_issue that way is just a local variable defined nowhere, try specifying it as a working url with new_issue_url:
test 'logged in should get issues page' do
sign_in users :one
test_stop_id = 1
test_line_id = 1
get new_issue_url, stop_id: test_stop_id, line_id: test_line_id
assert_equal test_stop_id, #issue.stop_id
assert_equal test_line_id, #issue.line_id
assert_response :success
end
I am trying to test my sign up page but I don't understand why it doesn't work.
#SIGN UP FLOWS
test "signed up user with valid info is redirected to root" do
get root_path
post user_registration_path, :user_email => "asasas#as.com", :user_password => "asasas", :user_password_confirmation => "asasas"
follow_redirect!
assert_equal "/", path
end
I have also tried :
#SIGN UP FLOWS
test "signed up user with valid info is redirected to posts" do
get root_path
#david = User.create(email: "aaaasadasadaddamdaaa#aaa.com", password: Devise::Encryptor.digest(User, "aaaaaa"))
post user_registration_path, 'user[email]' => #david.email, 'user[password]' => #david.password, 'user[password_confirmation]' => #david.password
assert_equal "/", path
end
Instead of going to my root which is / It stays on the same page. I tried to do follow_redirect! but it gives me the error that reduction is not possible because RuntimeError: not a redirect! 200 OK.
What am I doing wrong? I have looked at devise GitHub page but there is no information about registration testing.
I have a problem with unit testing in ruby on rails (rails v. 5.001). I use devise and cancancan for authorization. The user needs to login in a test unit, but how can I implement this without redirecting to http://www.example.com/users/sign_in? Here is the code:
appointments_controller_test.rb
require 'test_helper'
class AppointmentsControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
def sign_in(user)
post user_session_path \
"heiko#me.com" => user.email,
"hhdk#s0" => user.password
end
setup do
#heikoAppointment = appointments(:appointment_heiko)
#heiko = users(:user_heiko)
sign_in(#heiko)
end
test "should get index" do
get appointments_url
assert_response :success
end
test "should get new" do
sign_in(#heiko)
get new_appointment_url
assert_response :success
end
And here is the Error I get when I run the test:
Error:
AppointmentsControllerTest#test_should_get_new [C:/Users/Clemens/meindorfladen/Server/test/controllers/appointments_controller_test.rb:33]:
Expected response to be a <2XX: success>, but was a <302: Found> redirect to <http://www.example.com/users/sign_in>
The issue is your user is not confirmed.
You need to pass in
confirmed_at = Time.now
to the user you create. That will then stop you getting redirected.
I'm trying to ensure proper user access is maintained with Devise in Rails 4, and I'm having a hard time logging a user in in the test suite.
The simplest case:
require 'test_helper'
include Devise::TestHelpers
class SiteLayoutTest < ActionDispatch::IntegrationTest
def setup
#user = users(:test1)
end
test "logged in should get index" do
sign_in #user
get users_path
assert_response :success
assert_select "title", "User Index"
end
end
So far I've not done more really than just implement Devise and a Users controller with the appropriate actions.
I consistently get: NoMethodError: undefined method 'env' for nil:NilClass, referring specifically to the line containing sign_in #user and I can find other instances of people getting the same error, but never seem to find an actual solution to the problem I'm attempting to solve.
How do I log a user in with Devise in Rails 4 for testing purposes? Thanks.
EDIT:
fixtures/users.yml
test1:
id: '1'
email: 'test1#example.com'
encrypted_password: <%= Devise::Encryptor.digest(User, "password") %>
created_at: <%= Time.now - 6.minutes %>
updated_at: <%= Time.now - 4.minutes %>
SOLUTION IN SITU:
test "logged in should get index" do
post user_session_path, 'user[email]' => #user.email, 'user[password]' => 'password'
get users_path
assert_response :success
assert_select "title", "User Index"
end
This is from their docs:
"Do not use Devise::TestHelpers in integration tests."
You have to sign in manually. This is an example of a test for a website that does not allow users to get to the root path unless signed in. You can create a method in a support file that signs in the user manually and then call it whenever you want to sign in the user, so that you don't have to use this code every time you need to sign in a user.
require 'test_helper'
class UserFlowsTest < ActionDispatch::IntegrationTest
test "signed in user is redirected to root_path" do
get user_session_path
assert_equal 200, status
#david = User.create(email: "david#mail.com", password: Devise::Encryptor.digest(User, "helloworld"))
post user_session_path, 'user[email]' => #david.email, 'user[password]' => #david.password
follow_redirect!
assert_equal 200, status
assert_equal "/", path
end
test "user is redirected to sign in page when visiting home page" do
get "/"
assert_equal 302, status
follow_redirect!
assert_equal "/users/sign_in", path
assert_equal 200, status
end
end
EDIT: Just in case it's helpful in the future. You can use Warden Test Helpers for integration tests but the way above is a better test. This is a working example:
require 'test_helper'
include Warden::Test::Helpers
class UserFlowsTest < ActionDispatch::IntegrationTest
test "user can see home page after login" do
#david = User.create(email: "david#mail.com", password: Devise::Encryptor.digest(User, "helloworld"))
login_as(#david)
get "/"
assert_equal 200, status # User gets root_path because he loged in
assert_equal "/", path
logout
get "/"
assert_equal 302, status # User is redirected because he loged out
Warden.test_reset! #reset Warden after each example
end
end