Code Coverage in Ruby on Rails

I am using SonarCloud/GitHub, and my SonarCloud says my code coverage is at 28%, but when I run rspec on my code, it says 88%. I’m just trying to figure out the sequence of steps that SonarCloud is going through to determine its value, and what settings affect it.

My sonar.properties looks like this:

sonar.projectKey=REDACTED
sonar.host.url=https://sonarcloud.io
sonar.organization=REDACTED
sonar.sources=app
sonar.exclusions=app/assets//*,app/controllers/admin//,app/controllers/debug/**/,app/views/**/*
sonar.ruby.coverage.reportPaths=coverage/.resultset.json

I am BRAND NEW to SonarCloud, and someone else set this up previously. Is SonarCloud the one running the tests, or are they running elsewhere and results being pushed up? If the results are pushed, what is the command/mechanism that they get pushed up with? I really have no idea how Sonar works generally, so there is probably a ridiculously easy explanation. I just don’t even know where to start.

Hello @jb_61820,

I guess a good starting point is simply the documentation!

For coverage specifically, have a look here: Test Coverage & Execution

Specifically:

SonarSource analyzers do not run your tests or generate reports. They only import pre-generated reports.

In your case, the line sonar.ruby.coverage.reportPaths=coverage/.resultset.json is what you are looking for.

Now, concerning the difference, my bet is that you have files in your project not appearing in the SimpleCov report. It can be the case if you have other languages in the project for example, the other files will not be considered as covered, reducing the total amount.

I hope it leads you to a solution, and feel free to come back with more questions while you investigate further.

Best,
Quentin