gpt4 book ai didi

ruby-on-rails - Rack rSpec Controller Tests with Rack Middleware 问题

转载 作者:行者123 更新时间:2023-12-04 03:54:13 27 4
gpt4 key购买 nike

我在使用 rSpec 的 Controller API 进行测试时遇到了很大的麻烦。现在我正在使用中间件身份验证解决方案 ( Warden ),当我运行规范时,中间件添加的代理不存在,所有身份验证测试都抛出 NilPointerException在这个地方。

rSpec 似乎并没有故意将中间件添加到最终的应用程序中,我想知道是否有一种方法可以对 rSpec 进行猴子补丁以实现这一目标。

我已经用 Cucumber 测试了整个过程,但是这是对旧身份验证版本的重构,并且有几个 Controller 测试依赖于身份验证逻辑才能工作。

提前致谢。

最佳答案

看起来 Rails 2.x 的 rSpec Controller API 并没有真正在 Controller 调用上使用 Rack,所以这是不可能的。但是,David Chelimsky 指出了一个(未经证实的)解决方案。我会将他给我的答案放在 rSpec 用户邮件列表中。

Controller specs don't go through rack in rails 2, so not really.

There are integration specs, which wrap the rails integration test APIso you can do anything you can do in a rails integration test. Justmove the files over to spec/integration, convert the requests from"get :index" to "get 'path/to/index'"and you should be pretty muchgood to go.

Let us know how that works out.

Cheers,David

我最终在身份验证逻辑方法上模拟了 ApplicationController 以执行虚拟操作以使我的规范运行,我在 David 指出建议之前就这样做了,所以我不知道这是否有效。无论哪种方式,我都希望它能帮助处于相同情况的任何其他开发人员。

关于ruby-on-rails - Rack rSpec Controller Tests with Rack Middleware 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2225852/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com