我真不明白为什么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发布于 2009-08-26 04:09:37
基于user_sessions_controller create方法中的代码,该方法采用登录凭据的哈希,我能够在我的集成测试中让它像这样工作:
UserSession.create(:email => 'someone@example.com', :password => 'password')但不是通过以下方式:
UserSession.create(@user)发布于 2009-09-16 09:03:22
我也对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发布于 2009-09-03 16:29:36
我发现对于集成测试,我需要通过帖子登录:
setup do
post 'user_session', :user_session => {:email => 'someone@example.com', :password => 'password'}
end这正确地设置了会话,而上面提到的方法只在功能测试中对我有效。
https://stackoverflow.com/questions/1234920
复制相似问题