2009-08-05 195 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