【问题标题】:Integration testing with Authlogic?使用 Authlogic 进行集成测试?
【发布时间】:2009-08-05 18:25:55
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails authlogic


    【解决方案1】:

    我也在将 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
    

    【讨论】:

      【解决方案2】:

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

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

      但不是:

      UserSession.create(@user)
      

      【讨论】:

      • 谢谢。从 rdoc 中的这一行: UserSession.create(users(:whomever)) 我假设我可以传递一个@user obj。感谢您的帮助!
      • 嗯,根据这个:rdoc.info/rdoc/binarylogic/authlogic/blob/… 我应该能够做 UserSession.create(@user) 并让它工作...... wtf.
      【解决方案3】:

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

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

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

          UserSession.create!(@user)
          

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

          【讨论】:

          • 我使用的版本和你一样,这就是我在 irb 中得到的:(rdb:1) UserSession.create(User.find(25)) #""}> (rdb:1) UserSession.create(User.find(25)).save true 显然其他东西一定会干扰用户/会话模型
          • 请在函数中使用感叹号(!)再次测试。
          • 它在我的控制器上运行良好,我没有在 Rails 控制台上测试过。
          【解决方案6】:

          对于在 Google and the Greater Justice 中找到此帖子的人 - 您必须在 User 模型中设置 persitence_token 属性。例如。你可以打电话给@user.reset_persistence_token!,一切才刚刚开始。 :)

          【讨论】:

            【解决方案7】:

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

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

            【讨论】:

              【解决方案8】:

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

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

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

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

              【讨论】:

                【解决方案9】:

                看看rdoc

                【讨论】:

                • thx... 我在原始帖子中链接到 rdoc,并认为我正在关注其中的内容。这就是我发布的原因 - 因为我没有得到我期望的结果:)
                • 嗯。我的错误我没有看到。你可以投票否决。但它不会解决你的问题。
                • 好吧,这很有趣,但我也面临同样的问题。 :) 你解决了吗?
                • 哈哈...不幸的是不是waseem。你弄明白了吗?
                • 实际上我的要求发生了变化,我没有机会研究这个。
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2013-04-07
                • 2019-12-17
                • 2015-12-28
                • 2020-05-26
                • 2018-01-02
                • 2021-02-03
                • 2015-06-14
                相关资源
                最近更新 更多