27

I'm working on my very first simple Rails app. And now I just did my homework which I should do before anything else - RSpec tests. I delayed it on purpose because of no experience I even wasn't sure what for and how to do RSpec tests. Finally, I have probably most tests for my models and controllers and it's time for me to think about how much my tests cover the code.

Then I found rake stats, which shows me that:

+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          |   214 |   161 |       4 |      29 |   7 |     3 |
| Helpers              |    12 |    12 |       0 |       1 |   0 |    10 |
| Models               |    17 |    13 |       2 |       0 |   0 |     0 |
| Mailers              |     0 |     0 |       0 |       0 |   0 |     0 |
| Javascripts          |    29 |     3 |       0 |       1 |   0 |     1 |
| Libraries            |     0 |     0 |       0 |       0 |   0 |     0 |
| Helper specs         |    15 |     4 |       0 |       0 |   0 |     0 |
| Controller specs     |   170 |   137 |       0 |       0 |   0 |     0 |
| Model specs          |    78 |    65 |       0 |       0 |   0 |     0 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                |   535 |   395 |       6 |      31 |   5 |    10 |
+----------------------+-------+-------+---------+---------+-----+-------+
  Code LOC: 189     Test LOC: 206     Code to Test Ratio: 1:1.1

It shows how many Classes and Methods my controllers and models have. But what I'm missing here is how many are tested. I wish to have it instead of zeros there. At the same time would be nice to know which methods are not covered by tests. Is there a gem that provides that information or some other way to check it?

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
pawel7318
  • 3,383
  • 2
  • 28
  • 44

1 Answers1

40

I'd recommend SimpleCov for this.

Here's a nice starting configuration for it to put in your spec_helper.rb:

SimpleCov.start do
  add_filter '/test/'
  add_filter '/config/'
  add_filter '/vendor/'
  
  add_group 'Controllers', 'app/controllers'
  add_group 'Models', 'app/models'
  add_group 'Helpers', 'app/helpers'
  add_group 'Mailers', 'app/mailers'
end
# OPTIONAL
# This outputs the report to your public folder
# You will want to add this to .gitignore
SimpleCov.coverage_dir 'public/coverage'

This makes it ignore files in your test, config and vendor folders and groups controllers, models, helpers and mailers under their own tabs in the HTML report.

tirdadc
  • 4,603
  • 3
  • 38
  • 45
  • it looks impressive ! I already applied [this](https://github.com/colszowka/simplecov/issues/42#issuecomment-4440284) to my `spec_helper.rb` file but I can't find where config like the one you suggest should be placed (Spork.prefork or outside of it?) – pawel7318 Apr 06 '14 at 12:33
  • Good question, I'd try the prefork first and see if it works. – tirdadc Apr 06 '14 at 12:36
  • 1
    Actually, I'd try placing it in the `each_run` first and see if it works, that makes more sense. [This comment](https://github.com/colszowka/simplecov/issues/42#issuecomment-27539075) seems to have a thorough example. – tirdadc Apr 06 '14 at 12:42
  • yes, I put it to the `Spork.each_run` and looks it's working somehow as when I run `rspec controller/` I got `Coverage report generated for RSpec...` line at the end. But I can't go to the `coverage/index.html` - it's not present in my routes. Should I add it manually ? – pawel7318 Apr 06 '14 at 12:45
  • No, just set `SimpleCov.coverage_path = 'public/coverage'` and you should then be able to view the report at /coverage/ on your site. – tirdadc Apr 06 '14 at 12:54
  • Ahh ok, now I get it: it's generating static page so I can access it directly - no via `rails server`. – pawel7318 Apr 06 '14 at 12:57
  • 1
    So I double-checked and the correct setting is `SimpleCov.coverage_dir 'public/coverage'` right after your SimpleCov.start block. – tirdadc Apr 06 '14 at 13:05
  • Right, that way it can be served via the `rails server`. In my case I have whole app locally on my dev. All works great and it's fantastic. – pawel7318 Apr 06 '14 at 13:13
  • 3
    I don't think it's wise to have `SimpleCov.coverage_dir 'public/coverage'`. Someone might forget to add it to `.gitignore` and commit it. This will publish their code publicly online. – David Hooper Feb 15 '18 at 13:58
  • 5
    For those reading this in 2019, you don't need this custom configuration for the Rails app anymore. SimpleCov comes with "rails" profile out of the box. All you need is `SimpleCov.start 'rails'` in your `rails_helper.rb` (source: [their doc](https://github.com/colszowka/simplecov)) – Alexey Grinko Jul 27 '19 at 13:42