使用Authlogic进行集成测试?

时间:2009-08-05 18:25:56

标签: ruby-on-rails authlogic

对于我的生活,我不明白为什么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

9 个答案:

答案 0 :(得分:4)

我也在与Shoulda一起使用Authlogic(但是将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

答案 1 :(得分:3)

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

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

但不是:

UserSession.create(@user)

答案 2 :(得分:2)

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

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

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

答案 3 :(得分:2)

是的,我本周发现使用rspec:在功能规格中你可以模拟登录,只需要UserSession.create(@user)。但是如果你在集成规范中尝试它,它就不起作用。为了从集成规范登录,我发现我必须驱动表单(使用webrat),这显然会成为Facebook和OpenID登录等问题。

答案 4 :(得分:2)

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

UserSession.create!(@user)

使用Ruby v3.2.18和Authlogic v3.4.2。谢谢你指出我正确的方向。

答案 5 :(得分:0)

我正在使用Cucumber和James的解决方案以及以下修复程序为我工作:

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

答案 6 :(得分:0)

对于在Google和大正义中找到此帖子的人 - 您必须在用户模型中设置persitence_token属性。例如。你可以致电@user.reset_persistence_token!,一切都开始了。 :)

答案 7 :(得分:0)

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

此外,我必须修复 Cookie域

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

设置正确的cookie域后,一切都恢复了。

答案 8 :(得分:-3)

查看rdoc