如何测试Rails ERB模板的代码覆盖率? [英] How to test code coverage for Rails ERB templates?

查看:100
本文介绍了如何测试Rails ERB模板的代码覆盖率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是为旧版Rails应用程序构建测试套件。 simplecov 宝石非常适合查找需要测试覆盖范围(或可能完全未使用并且可以删除)的应用程序暗角。我只是通过在 test / test_helper.rb 的顶部包括以下内容来调用 simplecov

I'm just building a test suite for a legacy Rails app. The simplecov gem has been great for finding dark corners of the app which need test coverage (or which may be completely unused and OK to remove). I'm invoking simplecov simply by including the following at the top of test/test_helper.rb:

require 'simplecov'
SimpleCov.start('rails')

问题是这不会检查模板中嵌入的所有代码。其中有各种各样的垃圾,我敢肯定很多东西都可以删除,但是如果代码覆盖工具可以将我指向未使用的位,那将是非常不错的。

The problem is that this doesn't check all the code which is embedded in the templates. There is all kinds of junk in there, and I'm sure a lot of it could just be removed, but it would be really nice if a code-coverage tool could point me to the unused bits.

对Ruby 1.9的 Coverage 库进行了一些试验,使我相信,只有在模板以某种方式预编译为保存在Ruby源文件中的Ruby代码,然后 load ed或 require d,而不是直接加载已编译的模板与评估。或者,在编译过程中,可能有可能入侵ERB以将日志记录语句添加到每个模板的每一行。

Some experimentation with Ruby 1.9's Coverage library leads me to believe that it could only do the job if the templates were somehow pre-compiled to Ruby code, saved in Ruby source files, and then loaded or required, rather than loading the compiled templates directly with eval. OR, it might be possible to hack ERB to add logging statements to each line of each template during the compilation process.

是否有人对测量代码覆盖率有其他想法? ERB模板?您是否知道可以使用此工具的现成工具? (或者我必须成为构建和发布它的人吗?)

Does anyone have any other ideas how to measure code coverage of ERB templates? Do you know of an already-made tool which can do this? (Or will I have to be the one to build and release it?)

推荐答案

请参阅:
如何使用rspec,rails和simplecov?

答案是,您不能:

https://github.com/colszowka/simplecov/issues/38

我希望有人能够解决这个关键缺陷。
AFIK没有比Ruby 1.9的simplecov更好的了

It's critical flaw that I hope someone will address. AFIK there is nothing better than simplecov for Ruby 1.9

这篇关于如何测试Rails ERB模板的代码覆盖率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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