如何在 Ruby on Rails 和 Test::Unit 中测试控制器过滤器 [英] How to test Controller Filters in Ruby on Rails and Test::Unit

查看:31
本文介绍了如何在 Ruby on Rails 和 Test::Unit 中测试控制器过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 Ruby on Rails 中有一个包含许多过滤器的大型应用程序.其中一些过滤器可能很复杂.我正在寻找一种通过单元测试单独测试这些过滤器的方法.现在,我通过将它们与功能测试一起使用的操作来测试它们.感觉这不是正确的方式.
有没有人有这方面的建议或经验?

We have a large application in Ruby on Rails with many filters. Some of these filters can be complex. I am looking for a way to individually test these filters with a unit test. Right now I test them by testing them through an action that uses them with a functional test. This just doesn't feel like the right way.
Does anyone have advice or experience with this?

推荐答案

记住过滤器只是一种方法.
鉴于此:

Remember a filter is just a method.
Given this:

class SomeController
  before_filter :ensure_awesomeness

  ...
end

您没有理由不这样做:

SomeController.new.ensure_awesomeness

然后检查它是否调用了redirect_to或它应该做的任何事情

and then check that it calls redirect_to or whatever it's supposed to do

这篇关于如何在 Ruby on Rails 和 Test::Unit 中测试控制器过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆