如何测试Rails ERB模板的代码覆盖率?

时间:2021-11-05 22:30:29

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:

我只是为传统的Rails应用程序构建测试套件。 simplecov gem非常适合找到需要测试覆盖的应用程序的黑暗角落(或者可能完全未使用且可以删除)。我只是通过在test / test_helper.rb的顶部包含以下内容来调用simplecov:

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.

问题是,这不会检查嵌入在模板中的所有代码。那里有各种各样的垃圾,我确信它可以被删除很多,但如果代码覆盖工具可以指向未使用的位,那将是非常好的。

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.

使用Ruby 1.9的Coverage库进行的一些实验使我相信,如果模板以某种方式预编译为Ruby代码,保存在Ruby源文件中,然后加载或需要,而不是加载已编译的模板,它只能完成工作。直接用eval。或者,可能会破坏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?)

有没有人有任何其他想法如何衡量ERB模板的代码覆盖率?你知道一个可以做到这一点的已经制造的工具吗? (或者我必须成为构建和发布它的人吗?)

2 个解决方案

#1


9  

See: How do I get coverage for view specs with rspec, rails, and simplecov?

请参阅:如何使用rspec,rails和simplecov获取视图规范的覆盖范围?

The answer being, you can't:

答案是,你不能:

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

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

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

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

#2


-2  

try rake stats

尝试rake stats

it would generate a table having the coverage of each of the parts of the app

它会生成一个表格,其中包含应用程序各个部分的覆盖范围

#1


9  

See: How do I get coverage for view specs with rspec, rails, and simplecov?

请参阅:如何使用rspec,rails和simplecov获取视图规范的覆盖范围?

The answer being, you can't:

答案是,你不能:

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

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

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

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

#2


-2  

try rake stats

尝试rake stats

it would generate a table having the coverage of each of the parts of the app

它会生成一个表格,其中包含应用程序各个部分的覆盖范围