对 Warden 进行控制器测试
我在测试控制器和使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我为 Warden 编写了控制器测试助手。
http://kentaroimai.com/articles/1-controller-test-典狱长的帮手
I wrote controller test helpers for Warden.
http://kentaroimai.com/articles/1-controller-test-helpers-for-warden
尽管有很多例子告诉您在 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 throughrequest.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 atrequest.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.