对 Warden 进行控制器测试

发布于 2025-01-05 01:06:34 字数 819 浏览 1 评论 0原文

我在测试控制器和使用 Warden 时遇到问题。

所有示例都指向存根 request.env['warden']。当我调用 env['warden'] 时,这会导致我的控制器出现问题,然后返回 nil

举一个粗略的例子,使用这个:

request.env['warden'] = double(Warden, :authenticate => nil,
                                          :authenticate! => nil,
                                          :authenticated? => false)

和一个像这样的简单的过滤器:

before_filter do
  redirect_to new_user_session_url unless env['warden'].authenticated?
end

我得到一个nil

我刚刚设法使用 controller.env['warden'] = ... 让它工作,并且它工作了。 这是有道理的,因为它位于控制器级别,所以我想我的问题是它在我看过的所有示例中不起作用。

我的 spec_helper 中有这个:

config.include Warden::Test::Helpers

任何帮助都会很棒!

I'm having an issue with testing my controllers and using Warden.

All examples point at stubbing request.env['warden']. This causes issues in my controllers when I call env['warden'], which then returns nil.

For a crude example, using this:

request.env['warden'] = double(Warden, :authenticate => nil,
                                          :authenticate! => nil,
                                          :authenticated? => false)

And a simple before filter like this:

before_filter do
  redirect_to new_user_session_url unless env['warden'].authenticated?
end

I get a nil.

I just managed to get it working using controller.env['warden'] = ... and it works.
This makes sense, since it's sitting right at the controller level, so I guess my question is what wouldn't it work in the I've seen all examples.

I have this in my spec_helper:

config.include Warden::Test::Helpers

Any help would be great!

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

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

发布评论

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

评论(2

人间不值得 2025-01-12 01:06:35

尽管有很多例子告诉您在 Rails 应用程序中通过 env['warden'] 实现 Warden。这似乎是通过 request.env['warden'] 访问它的正确方法。

它通过在测试期间在我的控制器中提高 env 来发现这一点,并且结果总是nil

似乎在守望者中, https://github.com/ hassox/warden/blob/master/lib/warden/proxy.rb#L13
有一个用于机架环境的访问器,由于控制器测试中没有机架,因此在测试模式下不会存在该访问器。 请有人检查一下。

因此,在 RSpec 中存根 request.env 时,您的实现需要指向 request.env

在我看来,这似乎是一种必要的邪恶。但如果有人有很好的解释或解决办法,我很乐意继续这个讨论。

Despite many examples telling you to implement Warden through env['warden'] in your Rails app. It seems the correct way to access it through request.env['warden'].

It found this out by raising env in my controllers during tests, and this always came out nil.

It seems in Warden, https://github.com/hassox/warden/blob/master/lib/warden/proxy.rb#L13
There is an accessor for the rack environment, which won't exist in test mode due to the absence of Rack in controller tests. Please someone check this.

So when stubbing request.env in RSpec, your implementation needs to point at request.env.

It seems a necessary evil in my mind. But if there is anyone with a good explanation or work around, I'd love to continue this discussion.

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