如何在 cfwheels 中对重定向进行单元测试

发布于 2024-08-22 21:52:41 字数 371 浏览 4 评论 0原文

对执行重定向的控制器操作进行单元测试的正确方法是什么?

更新:我是 CFWheels 项目的核心开发人员。我问这个问题的全部原因是我目前正在努力增强我们拥有的内置测试框架,我想知道如何在 CFWheels 中处理和实现类似的东西,以使开发人员更容易测试他们的应用程序。

更新:我想出了如何测试这个。看来我们必须稍微重写轮子控制器的内部结构,以便在操作完成后并将其传递回调度程序后执行重定向。这种方法的唯一问题是,redirectTo() 之后的任何代码都将运行,除非您事先采取了适当的预防措施;这包括在redirectTo() 语句之后放置一个return 或在操作中放置条件检查以将redirectTo() 代码与其他代码隔离。它还涉及确保执行redirectTo() 后不会呈现任何其他内容。

What would be the correct way of unit testing a controller action that performs a redirect?

UPDATE: I'm a core developer on the CFWheels project. The whole reason I'm asking this question is currently I'm working on enhancing the built in testing framework we have and I'm wondering on how to approach and implement something like this in CFWheels to make it easier for developers to test their application.

UPDATE: I figured out how to test this. it seems that we will have to rewrite the wheels controller internals a bit to perform the redirect after the action completes and after it gets passed back to the dispatcher. the only thing with this approach is that any code after the redirectTo() would be ran EXCEPT if you take the appropriate precautions before hand; this includes either putting a return after the redirectTo() statement or putting conditional checks in your action to isolate the redirectTo() code from other code. It also involves making sure that nothing else is rendered once a redirectTo() is performed.

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

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

发布评论

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

评论(2

老娘不死你永远是小三 2024-08-29 21:52:41

如果将重定向机制分解为多个函数,这样的测试就会变得容易得多:a) 确定目标 URL 的函数和 b) 执行重定向的函数。

这样,您就可以非常轻松地测试 functionA,并且 functionA 无论如何都是您想要测试的。这是编写的代码。这就是你真正的逻辑所在。 FunctionB 不是您的代码;这是 ColdFusion 的核心,测试 CFLocation 没有多大意义。

如果您的任务是实现 100% 的测试覆盖率,那么您可以对 functionA 进行 10 次测试,在其中练习逻辑的所有不同分支,然后对 functionB 进行一次测试,以 Terry 建议的方式练习重定向;因此,通过这种方式,您只会得到 1 个慢速测试,而不是 10 个。

您可以将此方法推广到其他场景:文件系统、Web 服务、http、ftp、电子邮件等:“提取您可以控制并想要测试的内容来自您无法控制且不想测试的东西”

Stuff like this becomes a whole lot easier to test if you break apart the redirect mechanism into multiple functions: a) a function that determines the target URL and b) a function that performs the redirect.

This way, you can test functionA very easily, and functionA is the one you want to test anyway. It's the code you wrote. It's where your real logic is. FunctionB is not your code; it's ColdFusion guts, and there's not much sense in testing CFLocation.

If you're on some mission to achieve 100% test coverage, then you could spend 10 tests for functionA where you exercise all the different branches of your logic, and then one test for functionB which exercises the redirect in the manner that Terry suggests; so in this manner, you only get 1 slow test instead of 10.

You can generalize this approach for uses in other scenarios: file systems, webservices, http, ftp, email, etc: "Extract the stuff you can control and want to test from the stuff you can't control and don't want to test"

尸血腥色 2024-08-29 21:52:41

您可以使用 cfhttp 并将重定向设置为 false 来调用具有适当 URL 的控制器。然后测试生成的 cfhttp 结果结构,它应该包含有关重定向将带您前往何处的信息。

You could call the controller with the appropriate URL using cfhttp with redirect set to false. Then test the resulting cfhttp result struct, it should have info about where the redirect would take you.

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