Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
647 views
in Technique[技术] by (71.8m points)

ruby - How to test code coverage for Rails ERB templates?

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.

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.

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?)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

The answer being, you can't:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...