In this recipe, we will begin using the code coverage Gem with the built-in RubyMine tools to enhance your testing regimen and make your code even more robust.
We first need to add the Gem simplecov
to our Gemfile using the following steps:
Open this Gemfile and add the following line:
gem 'simplecov', :require => false, :group => :test
Navigate to Tools | Bunder | Install.
Now, all we have to do is to run our comprehensive set of tests that we created earlier using the coverage option:
- Select the rake test configuration and then hit the button that looks like "Run 'test' with Coverage".
If you followed the other recipes, then you probably got seven errors in your tests. Oops! Remember that the Borg are only seeking perfection, we have not yet arrived! We need to add some text fixtures that we removed earlier using the following steps:
Open the
test/fixtures/species.yml
file and add back the following fixtures:one: name: MyString identification: 1 assimilated: false two: name: MyString identification: 1 assimilated: false
Rerun the test with the coverage option and all of our tests should pass. A new panel will open on the right that has the results of our coverage, as shown in the following screenshot:
This shows that we don't have many of our lines of code actually tested. Let's generate a report, so we can see more detail:
Click on the Generate Coverage Report icon which looks like a square with a green arrow point up.
The next window will ask you where you want to save the file and gives the option of opening the resulting report in your browser.
Select this option and hit Save.
Your browser should now open with a report in which you can click on each line and get actual details of which lines of code have not been tested or executed:
Click on the file
app/controllers/species_controller.rb
.You will then get a screen like the following, which shows much more detail about exactly which lines were not tested.
The lines in green are tested and they show a little number on the right in a black circle that tells how many times that line of code was executed during the tests. The light red lines show which code has not been executed at all and are ripe for a new test to be written, so we can aspire to 100 percent code coverage:
Now you, the new RubyMine drone, can begin to write the missing tests and continue until you are a perfect 100 percent code coverage expert.
If you would like to read more information on SimpleCov, you can find it at https://github.com/colszowka/simplecov.