集成测试 Authlogic before_filter :require_admin_user 问题

发布于 2024-08-08 18:16:54 字数 835 浏览 4 评论 0原文

尽管创建了管理员用户会话,但我无法在集成测试中访问需要管理员用户的 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 - 所以不确定如何解决这个问题......

I'm not able to access urls in my integration test that require an admin user despite creating an admin user session. My test fails on a 302 error.

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

I've got the following in the test.log:

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

My fixtures file is named roles_users.yml as you would expect - so unsure of how to resolve this...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜`诱少女 2024-08-15 18:16:54

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

发生这种情况是因为您尚未登录。尝试:

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

或者用户 ID 实际是什么。

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

I think the new action is redirecting to the login screen.

This happens because you are not logged in. Try:

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

or what the user id actually is.

The first hash is the request hash, the second is the session hash.

萌吟 2024-08-15 18:16:54

您可能还需要将 Authlogic::TestCase 添加到您的类中。从我的应用程序(rspec,但相同的想法)

describe InvitationsController do
  include Authlogic::TestCase
  setup :activate_authlogic

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

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

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

You may also need to put include Authlogic::TestCase in your class. From my app (rspec, but same idea)

describe InvitationsController do
  include Authlogic::TestCase
  setup :activate_authlogic

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

Does your application require users to be activated? If so, is this user?

If that doesn't help. perhaps paste in some debug output, maybe an inspect of the created User.

听风吹 2024-08-15 18:16:54

我在集成测试中也遇到了 Authlogic 的问题,并进行了以下修复(来自 这篇文章)对我来说是一个成功的解决方法。

替换:

assert UserSession.create(users(:admin))

为:

post 'user_session', :user_session => {:email => '[email protected]', :password => 'password'}

I was also having a problem with Authlogic in Integration tests, and the following fix (from this post) was a successful workaround for me.

Replace:

assert UserSession.create(users(:admin))

With:

post 'user_session', :user_session => {:email => '[email protected]', :password => 'password'}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文