-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
Test helpers #71
Comments
Hi @dankimio! I think this could be a nice addition. What kinds of tests would you expect to use this in? |
Hello! I think both controller (integration) and system would be nice to have for starters, and would cover most cases. Devise has IntegrationHelpers. |
Since there is no help in this yet i do like this def passwordless_sign_in(resource)
session = Passwordless::Session.create!(
authenticatable: resource,
user_agent: 'Command Line',
remote_addr: 'unknown'
)
visit Passwordless::Engine.routes.url_helpers.token_sign_in_path(session.token)
end |
How would you do this in controller tests? |
You could just mock it. If you're using RSpec, something like def sign_in(user)
allow_any_instance_of(ApplicationController).to receive(:current_user) { user }
end |
@crispinheneise you could use @Uysim's code and substitute |
@scomma yes, I did use @Uysim's suggestion. Here's a module that can be included in module PasswordlessSupport
module TestCase
def passwordless_sign_out
delete Passwordless::Engine.routes.url_helpers.sign_out_path
end
def passwordless_sign_in(resource)
session = Passwordless::Session.create!(authenticatable: resource, user_agent: 'TestUnit', remote_addr: 'unknown')
get Passwordless::Engine.routes.url_helpers.token_sign_in_path(session.token)
end
end
module SystemTestCase
def passwordless_sign_in(resource)
session = Passwordless::Session.create!(authenticatable: resource, user_agent: 'TestUnit', remote_addr: 'unknown')
visit Passwordless::Engine.routes.url_helpers.token_sign_in_path(session.token)
end
end
end
class ActiveSupport::TestCase
include ::PasswordlessSupport::TestCase
end
class ActionDispatch::SystemTestCase
include ::PasswordlessSupport::SystemTestCase
end Any recommendations on DRY-ing up this module? |
Looks good, @crispinheneise! I don't see a reason to DRY anything up. Very short and succinct 👍 |
I've tried to use it. It works, but there is module PasswordlessSupport
module TestCase
def passwordless_sign_out
delete Passwordless::Engine.routes.url_helpers.sign_out_path
follow_redirect!
end
def passwordless_sign_in(resource)
session = Passwordless::Session.create!(authenticatable: resource, user_agent: 'TestUnit', remote_addr: 'unknown')
get Passwordless::Engine.routes.url_helpers.token_sign_in_path(session.token)
follow_redirect!
end
end
module SystemTestCase
def passwordless_sign_in(resource)
session = Passwordless::Session.create!(authenticatable: resource, user_agent: 'TestUnit', remote_addr: 'unknown')
visit Passwordless::Engine.routes.url_helpers.token_sign_in_path(session.token)
end
end
end
class ActiveSupport::TestCase
include ::PasswordlessSupport::TestCase
end
class ActionDispatch::SystemTestCase
include ::PasswordlessSupport::SystemTestCase
end |
Something like Devise's
sign_in @user
helper.See also #29.
The text was updated successfully, but these errors were encountered: