2009-08-05 174 views
10

对于我的生活,我不明白为什么Authlogic没有在这个集成测试中登录我。在使用此代码进行功能测试时,我没有遇到任何Authlogic记录的问题。根据authlogic rdocs(http://tinyurl.com/mb2fp2),模拟登录状态在功能&集成测试中是相同的,所以我很困惑。任何帮助深表感谢!Authlogic集成测试?

class TipsController < ApplicationController 
    before_filter :require_user, :only => [:destroy, :undelete] 
    def destroy 
    @tip = Tip.find(params[:id]) 

    if can_delete?(@tip) 

     @tip.destroy 

     set_flash("good", "Tip deleted. <a href=\"#{undelete_tip_url(@tip.id)}\">Undo?</a>") 
     respond_to do |format| 
     format.html { redirect_to city_path(@tip.city)} 
     end 
    else 
     set_flash("bad", "Seems like you can't delete this tip, sorry.") 
     respond_to do |format| 
     format.html { render :action => "show", :id => @tip} 
     end 
    end 
    end 
end 


class DeleteTipAndRender < ActionController::IntegrationTest 
    context "log user in" do 
    setup do 
     @user = create_user 
     @tip = create_tip 
    end 

    context "delete tip" do 
     setup do 
     activate_authlogic 
     UserSession.create(@user) 
     @us = UserSession.find 
     post "/tips/destroy", :id => @tip.id 
     end 

     should_redirect_to("city_path(@tip.city)"){city_path(@tip.city)} 
    end 
    end 
end 

回答

3

根据在user_sessions_controllercreate方法,这需要登录凭证的哈希代码,我能够做出像这样的工作在我的集成测试:

UserSession.create(:email => '[email protected]', :password => 'password') 

但与:

UserSession.create(@user) 
+1

谢谢。从rdoc中的这一行开始: UserSession.create(users(:whoome)) 我假设我可以传递@user obj。感谢帮助! – kareem 2009-08-28 11:57:03

+1

嗯根据本: http://rdoc.info/rdoc/binarylogic/authlogic/blob/73c4cccb38189f0e52e1e362992dfb9db7d1206f/Authlogic/Session/UnauthorizedRecord.html 我应该能够做到 UserSession.create(@user) 并让它工作... wtf。 – kareem 2009-08-31 21:47:30

-3

看看rdoc

+0

thx ...我链接到我原来的文章中的rdoc,并认为我遵循那里的内容。这就是为什么我张贴 - 因为我没有得到我期望的结果:) – kareem 2009-08-05 21:34:22

+0

嗯。我的错误我没有看到。你可以投票。它不会解决你的问题。 – Waseem 2009-08-06 03:21:58

+0

好吧,这很有趣,但我面临同样的问题。 :)你解决了吗? – Waseem 2009-08-15 20:07:28

2

我发现,集成测试,我需要通过邮寄登录:

setup do 
    post 'user_session', :user_session => {:email => '[email protected]', :password => 'password'} 
end 

这会正确设置会话,而上述方法仅适用于功能测试。

4

我也在使用Authlogic with Shoulda(但在factory_girl的顶部)。

我functionnal测试看起来像:

require 'test_helper' 

class LoansControllerTest < ActionController::TestCase 
[...] 

    context "as a signed-in user, with an active loan" do 
    setup do 
     @user = Factory(:user) 
     @user_session = UserSession.create(@user) 
     @loan = Factory(:loan, :ownership => Factory(:ownership, :user => @user)) 
    end 

    context "on GET to :index" do 
     setup do 
     get :index 
     end 

     should_respond_with_success 
    end 
    end 
end 

其实,你可以通过一个有效的用户UserSession,它在RDoc的也。 你也应该避免在每个控制器测试呼叫activate_authlogic:

ENV["RAILS_ENV"] = "test" 
require File.expand_path(File.dirname(__FILE__) + "/../config/environment") 
require 'test_help' 

class ActiveSupport::TestCase 
    [...] 
    # Add more helper methods to be used by all tests here... 
    include Authlogic::TestCase 

    def setup 
    activate_authlogic 
    end 
end 
2

是啊,我这个星期发现使用RSpec:在功能规格您模拟登录就好了W/UserSession.create(@user)。但是,如果您在集成规范中尝试这种方式,则不起作用。为了从集成规范登录,我发现我不得不驱动表单(使用webrat),这显然会对Facebook和OpenID登录等问题造成影响。

0

对于谁找到这篇文章在谷歌和更加公正的人 - 你必须设置persitence_token在用户模型属性。例如。你可以打电话@user.reset_persistence_token!,一切都开始工作。 :)

0

我有同样的问题,我可以通过手动注销修复(如其他已经回答)

此外,我不得不修复我的Cookie域

Rails使用www.example.com为它的测试,并且由于我在我的application.rb(通过config.cookie_domain)将cookie域设置为不同的值,登录后创建的会话cookie无法从后续请求访问。

设置正确的Cookie域后,一切都重新开始。

2

我不能让它与接受的答案一起工作,但很接近。我不得不感叹号添加到函数的末尾:

UserSession.create!(@user) 

它的工作与红宝石v3.2.18和Authlogic v3.4.2。谢谢你指出我在正确的方向。

+0

我使用的是与您相同的版本,这是我在irb中获得的内容: (rdb:1)UserSession.create(User.find(25)) # “}> (rdb:1)UserSession.create(User.find(25))。保存 真的很明显其他东西一定会干扰用户/会话模型 – prusswan 2014-06-25 03:09:52

+0

请使用函数中的感叹号(!)再次测试。 – ZombieBsAs 2014-06-26 17:36:43

+0

它在我的控制器上正常工作,我没有在轨道控制台上测试它。 – ZombieBsAs 2014-06-26 19:48:17