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 in purpose as because of no experience I even wasn't sure for what 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 covers 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 has no their tests. Is there a gem that provides that information or some other way to check it ?
This is the initial code for writing your first RSpec test. You need to require the rspec gem. Then you need to create a describe block to group all your tests together & to tell RSpec which class you are testing. Next is the it block: This is the test name, plus a way to group together all the components of the test itself.
On the Test menu, select Analyze Code Coverage for All Tests. You can also run code coverage from the Test Explorer tool window. After the tests have run, to see which lines have been run, choose Show Code Coverage Coloring in the Code Coverage Results window. By default, code that is covered by tests is highlighted in light blue.
RSpec Formatters. The default RSpec output is in the “progress” format. With this format you see dots (.) representing 1 passing test each, an F for a failed test (expected & actual don’t match), or an E for an error. But there are alternative formatting options you can use. Here’s a list: progress; documentation; json; html
The change matcher also works like this: The default RSpec output is in the “progress” format. With this format you see dots (.) representing 1 passing test each, an F for a failed test (expected & actual don’t match), or an E for an error.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With