13

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.

a.s.t.r.o
  • 3,261
  • 5
  • 34
  • 41

2 Answers2

18

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.

ybakos
  • 8,152
  • 7
  • 46
  • 74
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

Community
  • 1
  • 1
doesterr
  • 3,955
  • 19
  • 26
  • That is exactly what I'm trying to do using warden as you can see, to no avail. – a.s.t.r.o Mar 05 '13 at 09:35
  • The related answer helped me figuring out an alternative way that works, so thank you for that. – a.s.t.r.o Mar 05 '13 at 09:54
  • 2
    @AdnanDoric to know that you found a solution magically make my problems solve themselves. Awesome – Benjamin Bouchet Sep 01 '15 at 09:13
  • 1
    @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. – CJBrew Oct 30 '15 at 15:14