I'm beginner in Rails. I want to test how Devise works via Rspec requests. And I want to use user's login to test other bound controllers which desn't work without login of a user. I tied to do it via Devise recomendations for resting controllers Devise::TestHelpers inclusion in Rails 5
But I got error @request.env undefined method `env' for nil:NilClass
. I added all possible configurations in rails_helper.rb:
config.include Devise::Test::IntegrationHelpers, type: :request
config.include Devise::Test::IntegrationHelpers, type: :controller
config.include Devise::Test::IntegrationHelpers, type: :feature
Maybe it is way for testing controllers and doesn't satisfy for requests. Enyway I decided to use other direct methods to go on links: get, post. I have support/devise_helper.rb for registration, confirmation and login of a user.
def register_and_confirm_user(user)
get new_user_registration_path
post user_registration_path, :params => { :user => {username: user.username, email: user.email, password: user.password}}
ctoken = last_email.body.match(/confirmation_token=[\w|\-]*/)
get "/users/confirmation?#{ctoken}"
end
def login_in(user)
get new_user_session_path
post user_session_path, :params => { :user => { email: user.email, password: user.password }}
end
Perhaps it isn't the best way, but it doesn't return errors besides returning of a response:
Completed 302 Found in 12ms (ActiveRecord: 0.5ms | Allocations: 1579)
<html><body>You are being <a href="http://www.example.com/">redirected</a>.</body></html>
It happens with start test:
require 'rails_helper'
RSpec.describe "Devise", type: :request do
describe "session's methods" do
before(:each) do
@user = create(:user_first)
register_and_confirm_user(@user)
login_in(@user)
end
it 'signs in' do
puts response.body
end
end
end
In response of Rails I see that methods of registration, confirmation, ligin are works.Screen of response from Rails console The error, I think, in redirect after sign the user in. I don't know how it can be solve.
I use Rails 7.0.3.1, Devise 4.8.1. I will be very glad for any idea!