10

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

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
4

9 回答 9

4

我还在将 Authlogic 与 Shoulda 一起使用(但在上面使用 factory_girl)。

我的功能测试看起来像:

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
于 2009-09-16T09:03:22.080 回答
3

基于该user_sessions_controller create方法中的代码,它采用登录凭据的哈希值,我能够在我的集成测试中使其像这样工作:

UserSession.create(:email => 'someone@example.com', :password => 'password')

但不是:

UserSession.create(@user)
于 2009-08-26T04:09:36.997 回答
2

我发现对于集成测试,我需要通过帖子登录:

setup do
  post 'user_session', :user_session => {:email => 'someone@example.com', :password => 'password'}
end

这可以正确设置会话,而上面提到的方法仅适用于我在功能测试中。

于 2009-09-03T16:29:36.020 回答
2

是的,我本周发现使用 rspec: 在功能规范中,您可以很好地模拟登录 w/ UserSession.create(@user)。但是,如果您在集成规范中尝试这样做,它就不起作用。为了从集成规范登录,我发现我必须驱动表单(使用 webrat),这对于 Facebook 和 OpenID 登录之类的东西来说显然是个问题。

于 2009-10-02T05:05:49.113 回答
2

我无法使用已接受的答案,但很接近。我不得不将感叹号添加到函数的末尾:

UserSession.create!(@user)

它适用于 Ruby v3.2.18 和 Authlogic v3.4.2。感谢您为我指出正确的方向。

于 2014-06-08T21:21:42.750 回答
1

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

于 2010-10-28T07:47:37.250 回答
0

我正在使用 Cucumber 和 James 的解决方案以及对我有用的以下修复:

https://webrat.lighthouseapp.com/projects/10503/tickets/383-reset_session-and-webrat-dont-play-nicely-with-one-another

于 2010-10-16T22:03:41.507 回答
0

我有同样的问题,我可以通过手动登录来解决它(正如其他人已经回答的那样)

此外,我必须修复我的cookie 域

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

设置正确的 cookie 域后,一切正常。

于 2012-09-04T08:55:41.207 回答
-3

看看rdoc

于 2009-08-05T19:31:41.010 回答