racket-mock icon indicating copy to clipboard operation
racket-mock copied to clipboard

check-mock-num-calls prints expected and actual

Open vdloo opened this issue 9 years ago • 6 comments
trafficstars

same as https://github.com/jackfirth/racket-mock/pull/19 but for check-mock-num-calls expected-num-calls

before:

test that displayln is called 2 times
FAILURE
name:       check-mock-num-calls
location:   /home/vdloo/.racket/6.2.1/pkgs/jack-mock/mock/private/check.rkt:11:4
params:     2
#<procedure:...t/private/kw.rkt:213:14>
Check failure

after:

test that displayln is called 2 times
FAILURE
name:       check-mock-num-calls
location:   /home/vdloo/.racket/6.2.1/pkgs/jack-mock/mock/private/check.rkt:11:4
actual:     1
expected:   2
Actual amount of calls did not match expected

vdloo avatar Apr 09 '16 12:04 vdloo

Coverage Status

Coverage decreased (-1.8%) to 92.019% when pulling d57bba6007a3771939268f4c894e940a6236bb36 on vdloo:check-mock-num-calls-displays-expect-and-actual-nr-of-calls into 4caa5f620423a0c950dee16b2026811f385a8c3c on jackfirth:master.

coveralls avatar Apr 09 '16 12:04 coveralls

Could you add a test verifying that the check info is added? The current-check-handler parameter should allow you to get access to the check info stack in a test.

jackfirth avatar Apr 10 '16 08:04 jackfirth

I've come up with something like this:

#lang sweet-exp racket

require rackunit
        mock

(module+ test
  (test-case
    "check-mock-num-calls does not fail when amount of calls is equal"
    (check-mock-num-calls 0 (void-mock)))

  (test-case
    "current-check-handler is used by check-mock-num-calls"
    (check-eq?
      (let/ec escape
        (parameterize ([current-check-handler (λ (_) (escape 'foo))])
          (check-mock-num-calls 1 (void-mock))))
      'foo)))

similar to how they do it here but the inner check-mock-num-calls isn't properly caught so the test still fails, I'll have to play around with it some more.

raco test check-tests.rkt 
raco test: (submod "check-tests.rkt" test)
1/3 test failures

vdloo avatar Apr 10 '16 10:04 vdloo

I've tested this out:

(parameterize ([current-check-handler raise])
  (check-true #f))

This causes the exn:test:check to be raised instead of printed, so you can inspect it. So maybe something like this?

(define (has-info? check-failure info-name)
  (member info-name (map check-info-name (exn:test:check-stack check-failure))))

(check-exn
 (lambda (e) (has-info? e 'name))
 (thunk
  (parameterize ([current-check-handler raise])
    (check-true #f))))

check-info values are opaque so equal? doesn't work on them, which is unfortunate.

jackfirth avatar Apr 10 '16 20:04 jackfirth

Mmm, I don't understand why the inner checks still fail even though we raise them with the parameterize and wrap them in check-exn instead of printing them. The outer checks that test if the right info-names are in the check-stack do succeed. Do you have any idea?

vdloo avatar Apr 11 '16 07:04 vdloo

I'm not sure. Maybe current-check-around can be used to prevent it registering a failure? Might be worthwhile opening an issue in the rackunit repo about how to test custom checks.

jackfirth avatar Apr 11 '16 08:04 jackfirth