branch: elpa/buttercup commit 7cdb2191767f051c990b8f6ea76e94f6377d0ad0 Author: Jorgen Schaefer <cont...@jorgenschaefer.de> Commit: Jorgen Schaefer <cont...@jorgenschaefer.de>
Spies: :and-call-fake --- README.md | 30 ++++++++++++++++++++++++++++++ buttercup-test.el | 14 ++++++++++++++ buttercup.el | 4 ++++ 3 files changed, 48 insertions(+) diff --git a/README.md b/README.md index 70c6bbd..8eaad06 100644 --- a/README.md +++ b/README.md @@ -402,6 +402,36 @@ spied-on function should return. (expect fetched-bar :to-equal 745)))) ``` +### Spies: `:and-call-fake` + +The keyword argument `:and-call-fake` delegates calls to a supplied +function. + +```Lisp +(describe "A spy, when configured with an alternate implementation" + (let (bar set-bar get-bar fetched-bar) + (before-each + (fset 'set-bar (lambda (val) + (setq bar val))) + (fset 'get-bar (lambda () + bar)) + + (spy-on 'get-bar :and-call-fake (lambda () 1001)) + + (set-bar 123) + (setq fetched-bar (get-bar))) + + (it "tracks that the spy was called" + (expect 'get-bar :to-have-been-called)) + + (it "should not affect other functions" + (expect bar :to-equal 123)) + + (it "when called returns the requested value" + (expect fetched-bar :to-equal 1001)))) +``` + + ## Test Runners Evaluating `describe` forms just stores the suites. You need to use a diff --git a/buttercup-test.el b/buttercup-test.el index 5fe85b8..3fc2119 100644 --- a/buttercup-test.el +++ b/buttercup-test.el @@ -405,3 +405,17 @@ (expect (test-function 2 3) :to-equal 23))) + +(describe "The :and-call-fake keyword functionality" + (before-each + (spy-on 'test-function :and-call-fake (lambda (a b) 1001))) + + (it "tracks calls to the function" + (test-function 42 23) + + (expect 'test-function :to-have-been-called)) + + (it "returns the specified value" + (expect (test-function 2 3) + :to-equal + 1001))) diff --git a/buttercup.el b/buttercup.el index 72f02c7..83e3c6e 100644 --- a/buttercup.el +++ b/buttercup.el @@ -380,6 +380,10 @@ A disabled spec is not run." (setq new-value (lambda (&rest args) (buttercup--spy-add-call new-value args) arg))) + ((eq keyword :and-call-fake) + (setq new-value (lambda (&rest args) + (buttercup--spy-add-call new-value args) + (apply arg args)))) ((not keyword) (setq new-value (lambda (&rest args) (buttercup--spy-add-call new-value args)