Forum: Ruby Coverage report is different between load and require?

88112c5766888ac86c9b4336c9506fa2?d=identicon&s=25 Mark Maglana (mark_m46)
on 2013-08-31 21:47
I'm trying to understand why using load vs require results in different
coverage reports.

I'm writing a gem with dynamically loaded code that was previously not
detected by SimpleCov. I was able to partially fix this by rewriting the
code such that I can use Kernel::load instead of Object#instance_eval.
The remaining problem was that instance method definitions in my code
were reported by SimpleCov as not executed even though I clearly did.
For instance:

Here's a sample (Notice how #url and #headers are red):

Here's the test for the above code:

Kernel.load(path, true) and Kernel.load(path, false) yielded the same
results. Then I realized that, with the recent changes I made, I can
also actually use require. When I did that, here are the updated
coverage report for that file: So
the problem was solved as far as I was concerned.

My question is why the difference in the report when using load instead
of require?

P.S. Additional discussion on the original problem may be found in this
88112c5766888ac86c9b4336c9506fa2?d=identicon&s=25 Mark Maglana (mark_m46)
on 2013-09-01 00:11
Answering my own question. :-)

I loaded the files before checking if it was already loaded previously
(stupid mistake). Whereas require doesn't load the same file more than
once, load happily reloads code that was load earlier, the Coverage
module reports stats for that and since that newly reloaded code is not
executed, SimpleCov also reports that it didn't run.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.