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

1

20

I have a project in which I'm using rails 3.2.3, rspec 2.9.0, and simplecov 0.6.1, all seem to be the latest gems.

I'm getting code coverage results for my controllers and models, but not my views. I've tried adding the group in my simplecov setup:

SimpleCov.start 'rails' do
  add_group 'Views', 'app/views'
end

And even explicitly saying I want .erb files included:

SimpleCov.start 'rails' do
  add_group 'Views', 'app/views/*/*.erb'
end

...but no dice. The "Views" group is there in my coverage results, but no files are listed there.

Has anyone gotten this working?

Conjurer answered 6/4, 2012 at 23:44 Comment(1)
Does this answer your question? How to test code coverage for Rails ERB templates?Rhaetia
C
18

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

Conjurer answered 9/4, 2012 at 18:58 Comment(4)
It's a shame a work around hasn't been found yet. Any alternative tools found?Simonides
At the same time, we should try to move all logic away from the views and rely on presenters when needed. Integration testing and tools like capybara might help too.Habergeon
@AbePetrillo, I haven't found any alternatives. I cover some of this with Cucumber tests in my latest app, but mostly I'm agreeing with the conclusion that if there's something in my view worth testing, it should probably be in a helper instead.Conjurer
Sometimes there is no getting away from conditional logic in views, such as each blocks.Pucker

© 2022 - 2024 — McMap. All rights reserved.