This package provides some useful stubbing and mocking macros for unit testing. Used when specified functions in a test should not be computed but should instead return a provided constant value.
This library was called cl-mock before but is/was NOT the library called "cl-mock" on quicklisp. I have now changed the name.
(in-package :cl-user) (uiop:define-package :my-project (:use :closer-common-lisp :prove :mockingbird)) (in-package :my-project) (defun foo (x) x) (defun bar (x y) (+ x (foo x))) (with-stubs ((foo 10)) (is (foo 1 2) 10)) ;; --> T (is (bar 1 2) 2)) ;; --> T Only lexically stubbed! (with-dynamic-stubs ((foo 10)) (is (foo 1 2) 10)) ;; --> T (is (bar 1 2) 11)) ;; --> T Dynamically stubbed! (with-mocks (foo bar) (is (foo 5) nil) ;; --> T (is (bar 10) nil)) ;; --> T Args dont need to match! (with-dynamic-mocks (foo bar) ...)
The arguments passed to mocked or stubbed functions are also saved.
(with-stubs ((foo 5)) (foo 4 5) (call-times-for 'foo) ;; --> 1 (verify-call-times-for 'foo 1) ;; --> T (nth-mock-args-for 1 'foo) ;; --> '(4 5) (verify-nth-call-args-for 1 'foo 4 5) ;; --> T (verify-first-call-args-for 'foo 4 5) ;; --> T (clear-calls)) ;; --> no-value
These also work for the dynamic and mocking variants.
It is also possible to mock/stub individual methods.
(with-method-stubs ((foo (x y) 'is-stubbed) (foo ((x aclass) (y aclass)) 'aclass-stubbed) ...)
The calls to methods are currently NOT saved so the above verification functions can not be used.
Clone this repository and put into asdf load path then
To run tests:
- Christopher Eames (Chream) (email@example.com)
Copyright (c) 2016 Christopher Eames (Chream) (firstname.lastname@example.org)