Testing user_session using Authlogic and ACL9 yields weird errors

10 views
Skip to first unread message

buddybeers.com

unread,
Jul 19, 2010, 8:45:16 AM7/19/10
to acl9-discuss
It seems as if ACL9 is obstructing some pretty straightforward
functional tests.

User_sessions Controller:

access_control do
allow anonymous, :to => [:new, :create]
allow logged_in, :to => [:destroy]
end

def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:notice] = "Login successful!"
redirect_back_or_default account_url
else
render :action => :new
end
end

User_sessions_controller Test:

context "POST to CREATE" do
context "when logged out" do
setup { Factory(:user) }

context "and posting correct attributes" do
setup { post :create, :user_session => {:login =>
'foobar', :password => 'password'}}
should assign_to(:user_session)
should assign_to(:current_user)
should respond_with(:redirect)
should redirect_to("the user's account URL"){ account_url }
should set_the_flash.to("Login successful!")
end
end
end

Yields these errors:

1) Failure:
test: POST to CREATE when logged out and posting correct attributes
should assign @user_session. (UserSessionsControllerTest)
[shoulda (2.11.1) lib/shoulda/assertions.rb:59:in `assert_accepts'
shoulda (2.11.1) lib/shoulda/context.rb:324:in
`__bind_1279543408_123013'
shoulda (2.11.1) lib/shoulda/context.rb:382:in `call'
shoulda (2.11.1) lib/shoulda/context.rb:382:in `test: POST to
CREATE when logged out and posting correct attributes should assign
@user_session. '
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/
active_support/testing/setup_and_teardown.rb:62:in `__send__'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/
active_support/testing/setup_and_teardown.rb:62:in `run']:
Expected action to assign a value for @user_session

2) Failure:
test: POST to CREATE when logged out and posting correct attributes
should set the flash to "Login successful!".
(UserSessionsControllerTest)
[shoulda (2.11.1) lib/shoulda/assertions.rb:59:in `assert_accepts'
shoulda (2.11.1) lib/shoulda/context.rb:324:in
`__bind_1279543408_354809'
shoulda (2.11.1) lib/shoulda/context.rb:382:in `call'
shoulda (2.11.1) lib/shoulda/context.rb:382:in `test: POST to
CREATE when logged out and posting correct attributes should set the
flash to "Login successful!". '
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/
active_support/testing/setup_and_teardown.rb:62:in `__send__'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/
active_support/testing/setup_and_teardown.rb:62:in `run']:
Expected the flash to be set to "Login successful!", but was
{:error=>"You do not have access to this page"}

3) Failure:
test: POST to CREATE when logged out and posting incorrect attributes
should assign @user_session. (UserSessionsControllerTest)
[shoulda (2.11.1) lib/shoulda/assertions.rb:59:in `assert_accepts'
shoulda (2.11.1) lib/shoulda/context.rb:324:in
`__bind_1279543408_431012'
shoulda (2.11.1) lib/shoulda/context.rb:382:in `call'
shoulda (2.11.1) lib/shoulda/context.rb:382:in `test: POST to
CREATE when logged out and posting incorrect attributes should assign
@user_session. '
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/
active_support/testing/setup_and_teardown.rb:62:in `__send__'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/
active_support/testing/setup_and_teardown.rb:62:in `run']:
Expected action to assign a value for @user_session

4) Error:
test: POST to CREATE when logged out and posting incorrect attributes
should have errors. (UserSessionsControllerTest):
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.errors
/test/functional/user_sessions_controller_test.rb:55:in
`__bind_1279543408_504644'
shoulda (2.11.1) lib/shoulda/context.rb:382:in `call'
shoulda (2.11.1) lib/shoulda/context.rb:382:in `test: POST to
CREATE when logged out and posting incorrect attributes should have
errors. '
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/
testing/setup_and_teardown.rb:62:in `__send__'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/active_support/
testing/setup_and_teardown.rb:62:in `run'

5) Failure:
test: POST to CREATE when logged out and posting incorrect attributes
should render template new. (UserSessionsControllerTest)
[shoulda (2.11.1) lib/shoulda/assertions.rb:59:in `assert_accepts'
shoulda (2.11.1) lib/shoulda/context.rb:324:in
`__bind_1279543408_579085'
shoulda (2.11.1) lib/shoulda/context.rb:382:in `call'
shoulda (2.11.1) lib/shoulda/context.rb:382:in `test: POST to
CREATE when logged out and posting incorrect attributes should render
template new. '
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/
active_support/testing/setup_and_teardown.rb:62:in `__send__'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/
active_support/testing/setup_and_teardown.rb:62:in `run']:
expecting <"new"> but rendering with <"">

6) Failure:
test: POST to CREATE when logged out and posting incorrect attributes
should respond with 200. (UserSessionsControllerTest)
[shoulda (2.11.1) lib/shoulda/assertions.rb:59:in `assert_accepts'
shoulda (2.11.1) lib/shoulda/context.rb:324:in
`__bind_1279543408_661117'
shoulda (2.11.1) lib/shoulda/context.rb:382:in `call'
shoulda (2.11.1) lib/shoulda/context.rb:382:in `test: POST to
CREATE when logged out and posting incorrect attributes should respond
with 200. '
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/
active_support/testing/setup_and_teardown.rb:62:in `__send__'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.5/lib/
active_support/testing/setup_and_teardown.rb:62:in `run']:
Expected response to be a 200, but was 302

buddybeers.com

unread,
Aug 2, 2010, 3:33:29 PM8/2/10
to acl9-discuss
Turns out this all has to do with Authlogic logging in a user when you
create him. Be careful using Authlogic and Factories, because if you
Factory a user he gets logged in (unless you disable this in
Authlogic).

oleg dashevskii

unread,
Aug 3, 2010, 2:41:36 AM8/3/10
to acl9-d...@googlegroups.com
There's User#save_without_session_maintenance for that

03.08.2010, в 2:33, buddybeers.com написал(а):

Reply all
Reply to author
Forward
0 new messages