首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Authlogic进行集成测试?

使用Authlogic进行集成测试?
EN

Stack Overflow用户
提问于 2009-08-05 18:25:56
回答 9查看 5.4K关注 0票数 10

我真不明白为什么Authlogic不让我参加这个集成测试。我在Authlogic中使用这段代码登录功能测试时没有遇到任何问题。根据authlogic rdocs (http://tinyurl.com/mb2fp2)的说法,在功能和集成测试中模拟登录状态是相同的,所以我非常困惑。任何帮助都是非常感谢的!

代码语言:javascript
复制
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
EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2009-08-26 04:09:37

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

代码语言:javascript
复制
UserSession.create(:email => 'someone@example.com', :password => 'password')

但不是通过以下方式:

代码语言:javascript
复制
UserSession.create(@user)
票数 3
EN

Stack Overflow用户

发布于 2009-09-16 09:03:22

我也对Shoulda使用了Authlogic (但在上面使用了factory_girl )。

我的功能测试如下所示:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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
票数 4
EN

Stack Overflow用户

发布于 2009-09-03 16:29:36

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

代码语言:javascript
复制
setup do
  post 'user_session', :user_session => {:email => 'someone@example.com', :password => 'password'}
end

这正确地设置了会话,而上面提到的方法只在功能测试中对我有效。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1234920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档