2 Using Expectations with RackUnit
(require expect/rackunit) | package: expect |
This module defines how to use expectations with the rackunit testing framework. Included are custom checks and test forms that test values using expectations, as well as expectation-using replacements for the various built in checks provided by rackunit.
If you have an existing set of RackUnit tests, simply change (require rackunit) to (require expect/rackunit). Only checks are exported by expect/rackunit; if your tests use other exports of rackunit you’ll need to use only-in to import them from RackUnit.
procedure
(check-expect v exp [message]) → void?
v : any/c exp : any/c message : string? = ""
> (check-expect 1 (expect-pred number?)) > (check-expect 'foo (expect-pred number?))
--------------------
FAILURE
name: check-expect
location: eval:2:0
actual: 'foo
expected: number?
Expected a different kind of value
--------------------
> (check-expect #hash((a . (1 WRONG 3)) (b . (4 5 WRONG))) #hash((a . (1 2 3)) (b . (4 5 6))))
--------------------
FAILURE
name: check-expect
location: eval:3:0
actual: '#hash((a . (1 WRONG 3)) (b . (4 5 WRONG)))
fault:
summary: expected a different value
actual: 'WRONG
expected: equal? to 6
context:
in: value for key 'b
in: item at position 2
fault:
summary: expected a different value
actual: 'WRONG
expected: equal? to 2
context:
in: value for key 'a
in: item at position 1
Multiple faults found
--------------------
value
value
value
value
value
value
value
value
value
value
value
value
value
value
> (check-equal? '(1 2 3 foo 5) '(1 2 3 4 5))
--------------------
FAILURE
name: check-equal?
location: eval:1:0
subject: (1 2 3 foo 5)
actual: 'foo
expected: equal? to 4
context:
in: item at position 3
Expected a different value
--------------------
syntax
(test-subject maybe-name #:subject subject-expr expectation-expr ...)
maybe-name =
| name-str
subject-expr : any/c
expectation-expr : any/c
> (test-subject "addition" #:subject + (expect-call (arguments 1 2 3) (expect-return 6)) (expect-call (arguments) (expect-return 0)) (expect-call-exn (arguments 'foo) #rx"contract") (expect-call (arguments 5 10) (expect-return 16)))
--------------------
addition
FAILURE
name: test-expect
location: eval:1:0
subject: #<procedure:+>
actual: 15
expected: equal? to 16
context:
in: call with (arguments 5 10)
in: the return value
Expected a different value
--------------------
procedure
(fail-check/expect v exp) → void?
v : any/c exp : any/c
> (fail-check/expect 'foo (expect-pred number?)) Expected a different kind of value