-
-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NoMethodError: undefined method `session' for nil #207
Comments
These are your system tests? (I see |
The inegrations path has the I am including When I run a controller test within
Somewhere around here https://github.com/mikker/passwordless/blob/master/lib/passwordless/test_helpers.rb#L71 |
Does your project include https://github.com/rails/rails-controller-testing ? Maybe that adds something? |
I'm having this issue in a new Rails 7.1 project as well. Though I am not in a 7.0 project that also uses the passwords gem. I've only just started looking into it. Maybe it's an issue brought on by a newer version or Rails. |
For the record, I tried downgrading to several lower version of the passwordless gem, but that hasn't changed the behavior. The code breaks because as @mikker pointed out, this like is being executed: https://github.com/mikker/passwordless/blob/master/lib/passwordless/test_helpers.rb#L15 require 'passwordless/test_helpers'
class AlbumsControllerTest < ActionDispatch::IntegrationTest
test 'this is what breaks' do
passwordless_sign_in(users(:org_one_admin))
end
end |
Here is a bit more information about this error, specific to ActionDispatch::IntegrationTest. It doesn't seem to be an issue with controller tests, which you can still do with Rspec or the spec flavor of Minitest, but they have been phased out by the Rails team for a while. So if you're working with a fairly default test environment, you will be ActionDispatch::IntegrationTest when testing controllers. require 'test_helper'
module Manage
class AlbumsControllerTest < ActionDispatch::IntegrationTest
test 'this fails' do
# @request is nil until a request is made, or alternatively until @request is manually set.
# This is normal behavior for ActionDispatch::IntegrationTest, a request must be made in order
# for @request to be set to an instance of ActionDispatch::Request
# So you will always get a an error when ControllerHelpers.passwordless_sign_in is called first, because @request is nil
get "/"
# At this point @request is set to #<ActionDispatch::Request GET "https://www.example.com/" for 127.0.0.1>
# The passwordless helper can be called without error now,
# however what it does will not do you any good with ActionDispatch::IntegrationTest
passwordless_sign_in(users(:org_one_admin))
get new_manage_album_url(subdomain: organizations(:one).subdomain)
assert_response :success # fails because it's a 303 to the sign_in page
end
test 'this passes' do
def passwordless_sign_in(resource)
# Taken from Passwordless::TestHelpers::RequestTestCase
session = Passwordless::Session.create!(authenticatable: resource)
magic_link = Passwordless.context.path_for(
session,
action: "confirm",
id: session.to_param,
token: session.token
)
get(magic_link)
follow_redirect!
get new_manage_album_url(subdomain: organizations(:one).subdomain)
assert_response :success # passes 200
end
end I think its possible that including the Passwordless::TestHelpers::ControllerTestCase on ActiveSupport::TestCase might not work for some recent versions of Rails.
|
Nice digging 👍 do you have an idea for a fix? |
I’m not sure what the best approach is. Maybe:
It would be interesting to run this situation by Copilot for an opinion. |
The issue was resolved when I added this to my test_helper.rb if defined?(ActionDispatch::IntegrationTest)
ActionDispatch::IntegrationTest.send(:include, ::Passwordless::TestHelpers::RequestTestCase)
end So I guess that's the only change the gem source needs for it to work with a newer default rails configuration. Again, anyone using Rspec probably wouldn't encounter this issue, or people using the minitest DSL with |
I could submit a change after I get to a stopping point on my current project....you could assign this to me if you want so I won't forget. |
Appreciate it! 😅 |
this workaround works for me |
When running
passwordless_sign_in(@user)
, I get the errorNoMethodError: undefined method
session' for nil. It breaks [here](https://github.com/mikker/passwordless//blob/3b12614dd0ca377e7dc8c57ba9221a56538e5816/lib/passwordless/test_helpers.rb#L14-L15). It looks like
@requestis
nil`curiously, I am having errors in different versions of passwordless
The text was updated successfully, but these errors were encountered: