Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

problem with integration tests #36

Closed
chebyte opened this Issue · 6 comments

2 participants

@chebyte

hi there, I have a question, I using CAS and I want to make integration test with capybara and test units, the problem is how can I do the login with capybara, I'm trying to stubs the login I dont need make a real login so I was seeing here

https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara 

but I get

NoMethodError: undefined method `env' for nil:NilClass

I set the env to like this:

@request.env["devise.mapping"] = Devise.mappings[:user]

but returns the same error

so anyone knows what is the way for test this app with capybara through cas?

thanks

@nbudin
Owner

What the NoMethodError is telling you is that in the context of that statement, @request is nil. Where in your code did you put this statement?

@chebyte

this is my test_integration_helper

http://pastie.org/private/tqwnixwhqpvd4fglyxa

and this is my test

http://pastie.org/private/6kohz47qog4lelaqwj8pg

I tried with sign_in helper from devise and login_as helper from warden, I get the same error, nil env

do you see something wrong?

@nbudin
Owner

It looks like ActionDispatch::IntegrationTest doesn't have a @request object. (See https://github.com/rails/rails/blob/3de95fd9303ea2a2ffa5184f8cf32db63cb7f4ac/actionpack/lib/action_dispatch/testing/integration.rb for the source code.)

What it does have is a Session object as well as a method_missing? that delegates just about everything you do to that object. So you might be able to just do env["devise.mapping"] instead of @request.env["devise.mapping"], if the session exposes a env method (which I'm finding hard to tell just from the source code).

@chebyte

yeap seems that @request is nil, but in the devise code it is waiting for @request object, see this code is from devise, where the error happens

 def setup_controller_for_warden #:nodoc:
   @request.env['action_controller.instance'] = @controller
 end

is wierd, I tried puts @request into the test but I get nil too, maybe this class ActionDispatch::IntegrationTest not set this one?

I could stubs the login on devise and test the app with capybara even if I'm using cas?, I cant make real login cause the db login is in another app (the cas server)

@nbudin
Owner

Ah, makes sense.

What I actually do in my apps is somewhat more complicated. I use a little embedded CAS server called castronaut (my fork is here), which I run alongside the app using ShamRack. That way, devise_cas_authenticatable is making actual CAS calls to a real CAS server (that is running locally).

For an example, see here: https://github.com/nbudin/vellum/blob/master/features/support/cas.rb

@chebyte

looks very good, but I found the solution, the problem was that devise not supports integration test so I was loading devise helper on test_helper so it load these for all test, this is bad cause request is nil for capybara, for capybara you just need use warden helper like this guide says (https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara), so you just need load devise like this

class ActionController::TestCase
  include Devise::TestHelpers
end

and then follows the guide

thanks for all

thanks!

@chebyte chebyte closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.