current_user doesn't work with devise + rails 3 + rspec 2

959 views
Skip to first unread message

vo.x

unread,
Aug 9, 2010, 6:38:08 PM8/9/10
to Devise
Hello,

I have following spec:

describe "GET new" do
include Devise::TestHelpers

it "assigns a new project as @project" do
sign_in User.new

get :new
end
end

and corresponding method in controller:

class ProjectsController < ApplicationController
# GET /projects/new
# GET /projects/new.xml
def new
p current_user
end
end


While it works with my web browser and prints current_user, it doesn't
work with RSpec no matter what I'm trying to do.

I'm using following configuration:

C:\>ruby -v
ruby 1.9.2dev (2010-07-02) [i386-mingw32]

C:\>gem list

*** LOCAL GEMS ***

abstract (1.0.0)
actionmailer (3.0.0.rc)
actionpack (3.0.0.rc)
activemodel (3.0.0.rc)
activerecord (3.0.0.rc)
activeresource (3.0.0.rc)
activesupport (3.0.0.rc)
arel (0.4.0)
autotest (4.3.2)
bcrypt-ruby (2.1.2)
builder (2.1.2)
bundler (1.0.0.rc.1)
cancan (1.3.2)
cucumber (0.8.3)
devise (1.1.0, 1.1.rc2)
diff-lcs (1.1.2)
erubis (2.6.6)
flexmock (0.8.6)
gemcutter (0.5.0)
gherkin (2.0.2 i386-mingw32)
git (1.2.5)
i18n (0.4.1)
jeweler (1.4.0)
json_pure (1.4.3)
mail (2.2.5)
mime-types (1.16)
minitest (1.6.0)
mocha (0.9.8)
nokogiri (1.4.2.1 x86-mingw32)
polyglot (0.3.1)
rack (1.2.1, 1.1.0)
rack-mount (0.6.9, 0.6.6)
rack-test (0.5.4)
rails (3.0.0.rc)
railties (3.0.0.rc)
rake (0.8.7)
rcov (0.9.8)
rdoc (2.5.8)
rr (0.10.11)
rspec (2.0.0.beta.19)
rspec-core (2.0.0.beta.19)
rspec-expectations (2.0.0.beta.19)
rspec-mocks (2.0.0.beta.19)
rspec-rails (2.0.0.beta.19)
rubyforge (2.0.4)
sqlite3-ruby (1.3.1 x86-mingw32)
syntax (1.0.0)
term-ansicolor (1.0.5)
thor (0.14.0)
treetop (1.4.8)
trollop (1.16.2)
tzinfo (0.3.22)
warden (0.10.7)
watchr (0.6)
webrat (0.7.2.beta.1)

Thank you for your help.


Vit

José Valim

unread,
Aug 9, 2010, 8:04:42 PM8/9/10
to plataforma...@googlegroups.com, Devise
How can Devise persist in session a record that does not even exist in
the database? Try giving an existing record to sign_in and be sure it
is active? and confirmed?

Sent from my iPhone

vo.x

unread,
Aug 10, 2010, 9:26:22 AM8/10/10
to Devise
Ah, thank you for the tip. I will give it try. I was not able to find
the warden serialization overwrite in Devise before :)

But now, I have another question. Why did you choose to overwrite part
of warden in lib\devise\rails\warden_compat.rb instead use of Warden
API as described here: http://wiki.github.com/hassox/warden/serializers

Vit




On 10 srp, 02:04, José Valim <jose.va...@gmail.com> wrote:
> How can Devise persist in session a record that does not even exist in  
> the database? Try giving an existing record to sign_in and be sure it  
> is active? and confirmed?
>
> Sent from my iPhone
>

vo.x

unread,
Aug 10, 2010, 12:55:13 PM8/10/10
to Devise
btw it should be enough to add following code into the spec_helper.rb
to let my spec pass:

class Warden::SessionSerializer
def serialize(record)
record
end

def deserialize(keys)
keys
end
end

This stores directly the instance of user model in session instead of
its class and id, which is IMO just fine for unit tests. Later in
specs can be used:

@user = User.new
@user.stub!(:confirmed?).and_return(true) # This stubbing depends
on the model configuration.
sign_in @user

or alternatively:

@user = mock_model(User)
sign_in @user

May be it would be nice to add it to documentation or update the
helpers?

Vit


On 10 srp, 02:04, José Valim <jose.va...@gmail.com> wrote:
> How can Devise persist in session a record that does not even exist in  
> the database? Try giving an existing record to sign_in and be sure it  
> is active? and confirmed?
>
> Sent from my iPhone
>
Reply all
Reply to author
Forward
0 new messages