【问题标题】:Integration Testing Authlogic before_filter :require_admin_user Problem集成测试 Authlogic before_filter :require_admin_user 问题
【发布时间】:2009-10-28 13:05:32
【问题描述】:

尽管创建了管理员用户会话,但我无法在集成测试中访问需要管理员用户的 url。我的测试因 302 错误而失败。

class NewsItemsController < ApplicationController
    before_filter :require_admin_user, :except => [:show, :index, :feed]

    etc...

end

--test/inetgration/admin_stories.rb --

require 'test_helper'

class AdminStoriesTest < ActionController::IntegrationTest

  fixtures :all
    setup :activate_authlogic

  # if user is an admin he can create a new news_item
    def test_creating_a_news_item
        assert UserSession.create(users(:admin))
        get "news_items/new"
        assert_response :success
        #etc...
    end
end

我在 test.log 中有以下内容:

Unable to load roles_user, underlying cause no such file to load -- roles_user 

如您所料,我的设备文件被命名为 roles_users.yml - 所以不确定如何解决这个问题...

【问题讨论】:

  • 你有roles_user模型吗?
  • 不,我在角色和用户之间建立了 habtm 关联

标签: ruby-on-rails integration-testing authlogic


【解决方案1】:

我认为新操作正在重定向到登录屏幕。

这是因为您没有登录。尝试:

get "news_items/new", {}, { 'user_id' => 0 }

或者用户ID实际是什么。

第一个哈希是请求哈希,第二个是会话哈希。

【讨论】:

  • get "news_items/new", {}, { 'user_id' => '1' } 没有帮助 - 仍然出现 302 错误
  • 确实,这是为了restful_authentication
【解决方案2】:

您可能还需要在您的课程中添加 include Authlogic::TestCase。从我的应用程序(rspec,但同样的想法)

describe InvitationsController do
  include Authlogic::TestCase
  setup :activate_authlogic

  describe 'sending invitations' do
  .........

您的应用程序是否需要激活用户?如果有,是这个用户吗?

如果这没有帮助。也许粘贴一些调试输出,也许检查创建的用户。

【讨论】:

    【解决方案3】:

    我在集成测试中也遇到了 Authlogic 的问题,以下修复(来自 this post)对我来说是一个成功的解决方法。

    替换:

    assert UserSession.create(users(:admin))
    

    与:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多