GithubHelp home page GithubHelp logo

mockingbird's Introduction

Mockingbird

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.

Usage

(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.

Installation

Clone this repository and put into asdf load path then

(ql:quickload :mockingbird)

To run tests:

(ql:test-system :mockingbird)

Author

Copyright

Copyright (c) 2016 Christopher Eames (Chream) ([email protected])

mockingbird's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

pfdietz

mockingbird's Issues

Export pbs ? Can not see `with-stubs` nor `nth-mock-args-for` and others

Hi,
Nice to find this lib :) I added it on Cliki.

I installed it with quicklisp (ql:quickload :cl-mock)) and tried to use it but I can't find all functions:

  • (cl-mock:nth TAB => nothing
  • (cl-mock::nth TAB => (with double colons) nth-value-for, nth-value, nthcdr
  • I can't find with-stubs. (cl-mock:with- => with-mocks only, (cl-mock::with- => many more but no "with-stubs" nor with-dynamic-; can not find verify-*,โ€ฆ

so it looks like there is a pb with exported stuff (note that I have with-mocks). Also your use of uiop:define-package instead of asdf seems unusual to me but I can't judge.

Mocking functions defined in flet() or labels()

Mockingbird is a big help, but it only seems to allow me to mock a function that's been defined with defun(). Is it possible to mock a function that was defined (or overridden) in an flet() or labels()? When I try to do it, I see a message saying "does not have a defined original."

Thanks,
Rick

----------- Stack trace follows -------------

   UNDEFINED-STUB-FUNCTION: The defined stub function for TOURETZKY::INSTANT-LOSS-P does not have a defined original.

     1: ((FLET "H0" :IN #:DROP-THRU-TAG-69) The defined stub function for TOURETZKY::INSTANT-LOSS-P does not have a defined original.)
     2: (SB-KERNEL::%SIGNAL The defined stub function for TOURETZKY::INSTANT-LOSS-P does not have a defined original.)
     3: (ERROR UNDEFINED-STUB-FUNCTION NAME INSTANT-LOSS-P)
     4: (MOCKINGBIRD/SRC/FUNCTIONS:UNDEFINED-STUB-FUNCTION-ERROR INSTANT-LOSS-P)
     5: (MOCKINGBIRD/SRC/FUNCTIONS::DEFINED-FNS-BOUND-P ((INSTANT-LOSS-P T)))
     6: ((FLET "MAIN617" :IN #:DROP-THRU-TAG-69))
     7: ((FLET "MAIN67" :IN #:DROP-THRU-TAG-69))
     8: ((LAMBDA NIL :IN "/Users/rkawala/portacle/projects/touretzky/ch5-tests.lisp"))
     9: (ROVE/CORE/SUITE/PACKAGE:RUN-SUITE #S(ROVE/CORE/SUITE/PACKAGE::SUITE :NAME TOURETZKY :SETUP NIL :TEARDOWN NIL :BEFORE-HOOKS NIL :AFTER-HOOKS NIL :TESTS (EX5.6 ARRAY-POSITION ARRAY-LENGTH EX2.32 EX2.31 EX2.29 SEC2.15 EX2.23 EX2.22 EX2.21 EX2.20 EX2.19 EX2.18 SEC2.11.1 EX2.17 SEC2.10.4 EX2.15 EX2.13 EX2.12 EX2.9 EX2.8 EX2.7)))
     10: ((FLET "MAIN0" :IN ROVE/CORE/SUITE:RUN-SYSTEM-TESTS))
     11: (ROVE/CORE/SUITE:RUN-SYSTEM-TESTS TOURETZKY)
     12: (ROVE/MAIN:RUN TOURETZKY STYLE NIL ENV NIL)
     13: (SB-INT:SIMPLE-EVAL-IN-LEXENV (RUN TOURETZKY) #<NULL-LEXENV>)
     14: (SB-IMPL::SIMPLE-EVAL-PROGN-BODY ((RUN TOURETZKY) NIL) #<NULL-LEXENV>)
     15: (SB-INT:SIMPLE-EVAL-IN-LEXENV (PROGN (RUN TOURETZKY) NIL) #<NULL-LEXENV>)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.