i'm sure it's something i'm doing but here we go:
in test_helper.rb setup a login method:
def login(user = users(:one))
sign_in :user, user
# user
end
in a test of my Track controller i just simply put the
authenticate_user! in a before_filter in the Tracks_Controller.rb file
(boring scaffold stuff... nothing cool about this)
class TracksController < ApplicationController
before_filter :authenticate_user!
# GET /tracks
# GET /tracks.xml
def index
@tracks = Track.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @tracks }
end
end
end
in my Track_controller_test.rb file i do something like:
class TracksControllerTest < ActionController::TestCase
test "should get index" do
login
get :index
assert_response :success
assert_not_nil assigns(:tracks)
end
end
So i keep getting redirected which means i am not being authenticated
as a logged in user.
Your doc's talk about scope or resource for the sign_in helper
method. what are they supposed to be? @user means nothing to me in
terms of the docs friends, as it's an instance variable of user i
guess... but is @user an instance of?
Anyone able to help get me passed this authenticate_user! before
filter in my tests. (needless to say the code itself works in a
browser).
Have you already added Devise::TestHelpers to your test case? You can
do this by putting the following code in your test_helper.rb:
class ActionController::TestCase
include Devise::TestHelpers
end
And yes, @user is the instance of some valid user you have.
Yes i did put that in at the bottom of test_helper.rb.
I'm also using the devise gem 0.6.1 for reference.
here is a truncated version of my test_helper.rb file.
class ActiveSupport::TestCase
def login(user = users(:one))
sign_in user
end
end
class ActionController::TestCase
include Devise::TestHelpers
end
So i assume by passing a fixture to sign_in, and if that fixture is a
valid user, it should "pass" the authentication correct?
Here is my user.yml fixture i'm using... which i stole from my
User.first in script/console.
User.yml:
one:
email: m...@you.com
encrypted_password: be5d96b764da38099b7bdd255156e8b3c162c298
password_salt: onudpY9fYVuH6vtoSDiR
confirmation_token:
confirmed_at: <%= Time.now %>
confirmation_sent_at:
reset_password_token:
remember_token:
remember_created_at:
Am i doing something wrong code wise Carlos? Given all this info, how
would go about getting passed the before_filter?
thanks
On Dec 23, 11:44 am, "Carlos A. da Silva"
how would i see if sign_in actually is working?
On Dec 23, 12:13 pm, "Carlos A. da Silva"
@user = User.create!(
:email => 'us...@test.com',
:password => 'user123',
:password_confirmation => 'user123'
)
instead of using this fixture. I've created a fresh app here with the
following test:
class ProjectsControllerTest < ActionController::TestCase
include Devise::TestHelpers
def setup
user = User.create!(
:email => 'us...@test.com',
:password => 'user123',
:password_confirmation => 'user123'
)
sign_in user
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:projects)
end
end
And everythink is working fine.
sign_in user
get :index
assert_response :success
assert_not_nil assigns(:tracks)
end
------------------------
4) Error:
test_should_get_index(TracksControllerTest):
ActionView::TemplateError: Missing host to link to! Please
provide :host parameter or set default_url_options[:host]
On line #5 of /library/ruby/gems/1.8/gems/devise-0.7.4/app/views/
devise_mailer/confirmation_instructions.html.erb
2:
3: You can confirm your account through the link below:
4:
5: <%= link_to 'Confirm my account', confirmation_url
(@resource, :confirmation_token => @resource.confirmation_token) %>
(eval):20:in `user_confirmation_url'
(eval):11:in `send'
(eval):11:in `confirmation_url'
devise (0.7.4) app/views/devise_mailer/
confirmation_instructions.html.erb:5
devise (0.7.4) app/models/devise_mailer.rb:48:in
`render_with_scope'
devise (0.7.4) app/models/devise_mailer.rb:37:in `setup_mail'
devise (0.7.4) app/models/devise_mailer.rb:17:in
`confirmation_instructions'
devise (0.7.4) lib/devise/models/confirmable.rb:60:in
`send_confirmation_instructions'
/test/functional/tracks_controller_test.rb:12:in
`test_should_get_index'
...
rake (0.8.7) lib/rake/rake_test_loader.rb:5
So i added a test to see if i can see the current_user which would
mean it should pass, right? and it does.
Not sure where to go from here. without a before_filter passing it's
going to be hard to test.
On Dec 23, 12:31 pm, "Carlos A. da Silva"
<carlosantoniodasi...@gmail.com> wrote:
> Try creating a user by hand, something like:
>
> @user = User.create!(
> :email => 'u...@test.com',
> :password => 'user123',
> :password_confirmation => 'user123'
> )
>
> instead of using this fixture. I've created a fresh app here with the
> following test:
>
> class ProjectsControllerTest < ActionController::TestCase
> include Devise::TestHelpers
>
> def setup
> user = User.create!(
> :email => 'u...@test.com',
ActionView::TemplateError: Missing host to link to! Please
provide :host parameter or set default_url_options[:host]
says you have to configure :host to ActionMailer, as you're using
Confirmable Devise will send an email after creating the user. Setup
in your test env:
config.action_mailer.default_url_options = { :host => 'localhost:
3000' }
It should be done for each environment.
Please make sure you run script/generate devise_install before
starting testing devise also.
3) Failure:
test_should_get_edit(TracksControllerTest) [/test/functional/
tracks_controller_test.rb:44]:
Expected response to be a <:success>, but was <302>
I think it's creating the session fine and all that but there is
something in authenticate_user! that may not be passing it.
In your tests did you use the before_filter :authenticate_user! in
your projects controller?
On Dec 23, 12:58 pm, "Carlos A. da Silva"
http://dl.dropbox.com/u/1540806/devisetest.tar.gz
please take a look at the code. Try commenting the sign_in in tests,
etc to see the errors.
Loading a fixture didn't work for some reason, even a valid one, but
this ended up working instead:
def login
user = User.create!(
:email => 'ub...@test.com',
:password => 'user123',
:password_confirmation => 'user123'
)
sign_in user
user.confirm!
user
end
i use setup :login in my controller and it passes.
I also switched out the attributes above for a fixture, the same one i
posted up there, and the f@#cking thing works with user.confirm!
Very important step it appears.
HOWEVER, in your gunzip file you sent me devisetest... you didn't use
it and it worked. ODD.
while i care, i don't care too bad, since it works.
thanks all.
I'll blog about this more to get more of the word out there.
> ...
>
> read more »
thanks carlos and Jose.
> ...
>
> read more »
So I'm happy if you add description about 'confirm!' around Test
Helper section in Devise's Readme.rdoc.
thanks.
naoto
> > > > > >ConfirmableDevise will send an email after creating the user. Setup
> ...
>
> read more »