How to test code coverage for Rails ERB templates?
Asked Answered
D

3

16

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

Duff answered 23/10, 2012 at 12:56 Comment(0)
C
17

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

Contrariwise answered 29/10, 2012 at 2:45 Comment(1)
OK. Thanks. I'd accept this answer... but then I am thinking I may just have to create a solution to this problem myself, and if I do, that will become the accepted answer.Duff
B
4

Since a couple of years there is a new Gem available for Template coverage detection: covered:

https://github.com/ioquatix/covered

require 'covered/rspec'
COVERAGE=Summary rspec

Which will display the code coverage of all touched templates after the test run.

Brescia answered 9/3, 2023 at 8:14 Comment(0)
D
-4

try rake stats

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

Directrix answered 23/10, 2012 at 13:14 Comment(2)
What is your Rails version..it is working perfectly fine on Rails 3.2.3...try running rake -T and read out the descriptions of the tasks listed in it...Directrix
I don't want to know how many lines of code there are in my templates. I want to know how many of those lines are executed by my test suite. Please read the question carefully.Duff

© 2022 - 2024 — McMap. All rights reserved.