How to Test Code Coverage for Rails Erb Templates

How to test code coverage for Rails ERB templates?

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

Rcov show files wih 0% code coverage not considered

I found a solution that worked for me.

Allthough I'm not using jruby, this describes (and solves) my problem:

http://www.nearinfinity.com/blogs/brian_montgomery/finding_holes_in_rcov_and_jrub.html

I wrote a test, which just touches every class

How does one collect rcov-style information on the processing of erb templates?

I only actually absolutely need the view filenames as in most cases they will be executed in their entirety. My purpose is mainly to identify unused partials or templates. The following code outputs these to the screen.

module DeadCodeDetector                                                          
module Template
def set_extension_and_file_name_with_recording(use_full_path)
r = set_extension_and_file_name_without_recording(use_full_path)
puts "Included Template"
puts filename
puts "End Include"
puts
r
end

def self.included(base)
base.class_eval do
alias_method_chain :set_extension_and_file_name, :recording
end
end
end
end

ActionView::Template.send(:include, DeadCodeDetector::Template)

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

Apparently, the answer to this is "you can't", due to a limitation in the standard ruby coverage library:

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

How can I syntax check (not render) a Rails 3 ERB template file?

I have not dug much into either of these but you might try rails-erb-check (Git project) or this blog entry. I agree with shingara but the Blog Post describes a situation where this is useful and I wonder if you are in a similar position:

Diaspora is pretty fluid right now. This means we are have some green
tests, some missing tests, and other tests that check intent (not
implementation). In an ideal world, I suppose test cases would cover
all of our bases...

Until then, I've added a new task to my fork, check_syntax:all. This
breaks down further to the subtasks check_syntax:erb,
check_syntax:haml, check_syntax:haml_ruby, check_syntax:sass, and
check_syntax:yaml.



Related Topics



Leave a reply



Submit