RSpec, authenticating Devise user in request specs
Asked Answered
V

2

14

I'm trying to write RSpec request specs in order to test my service API and for that I need the user to be authenticated. I found some examples on the net but nothing works, for the moment I'm stuck with this:

require "spec_helper"

include Warden::Test::Helpers
Warden.test_mode!

describe "My requests" do

  it "creates an imaginary object" do
    user = FactoryGirl.create(:user)
    login_as(user, :scope => :user)
    post "/my_service", :my_data=> {:some => "data"}
    expect(response.body).to include("success")
  end

end

And the error I'm getting is:

 ArgumentError: uncaught throw :warden

Thank you for your help.

Vierno answered 4/3, 2013 at 21:34 Comment(0)
A
4

You need to actually sign in the user (i.e. the user needs to submit the login form, or at least do a POST on your login action) as explained here: Stubbing authentication in request spec

Antipathy answered 5/3, 2013 at 3:3 Comment(4)
That is exactly what I'm trying to do using warden as you can see, to no avail.Vierno
The related answer helped me figuring out an alternative way that works, so thank you for that.Vierno
@AdnanDoric to know that you found a solution magically make my problems solve themselves. AwesomeBes
@AdnanDoric if you could update your question with a few details that would be great. I am having the same issues here and any help would be great. I think that may be what Benjamin was getting at.Textbook
F
24

It is simplest to just:

spec/rails_helper.rb

RSpec.configure do |config|
  # ...
  config.include Devise::Test::IntegrationHelpers, type: :request
end

And just use sign_in in your request spec. This is the equivalent of declaring include Devise::Test::IntegrationHelpers in an system/feature spec or Rails system/controller test.

Doing it this way leads to a 'better' test.

Fluxmeter answered 19/5, 2018 at 23:39 Comment(1)
by applyting this solution im getting [#<Proc:0x00005624201db3f8@/home/humayun/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/warden-1.2.9/lib/warden/test/helpers.rb:19>]Dich
A
4

You need to actually sign in the user (i.e. the user needs to submit the login form, or at least do a POST on your login action) as explained here: Stubbing authentication in request spec

Antipathy answered 5/3, 2013 at 3:3 Comment(4)
That is exactly what I'm trying to do using warden as you can see, to no avail.Vierno
The related answer helped me figuring out an alternative way that works, so thank you for that.Vierno
@AdnanDoric to know that you found a solution magically make my problems solve themselves. AwesomeBes
@AdnanDoric if you could update your question with a few details that would be great. I am having the same issues here and any help would be great. I think that may be what Benjamin was getting at.Textbook

© 2022 - 2024 — McMap. All rights reserved.