branch: elpa/buttercup
commit 752619812cd9f0d4d954cc31a87e46607a72e3dc
Author: Jorgen Schaefer <[email protected]>
Commit: Jorgen Schaefer <[email protected]>
Spies: :and-throw-error
---
README.md | 21 +++++++++++++++++++++
buttercup-test.el | 9 +++++++++
buttercup.el | 4 ++++
3 files changed, 34 insertions(+)
diff --git a/README.md b/README.md
index 8eaad06..03c3f87 100644
--- a/README.md
+++ b/README.md
@@ -431,6 +431,27 @@ function.
(expect fetched-bar :to-equal 1001))))
```
+### Spies: `:and-throw-error`
+
+With the keyword argument `:and-throw-error`, all calls to the spy
+will `signal` the specified value as an error.
+
+```Lisp
+(describe "A spy, when configured to throw an error"
+ (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-throw-error 'error))
+
+ (it "throws the error"
+ (expect (lambda () (get-bar))
+ :to-throw 'error))))
+```
+
## Test Runners
diff --git a/buttercup-test.el b/buttercup-test.el
index 3fc2119..fc8d10c 100644
--- a/buttercup-test.el
+++ b/buttercup-test.el
@@ -419,3 +419,12 @@
(expect (test-function 2 3)
:to-equal
1001)))
+
+(describe "The :and-throw-error keyword functionality"
+ (before-each
+ (spy-on 'test-function :and-throw-error 'error))
+
+ (it "throws an error when called"
+ (expect (lambda () (test-function 1 2))
+ :to-throw
+ 'error "Stubbed error")))
diff --git a/buttercup.el b/buttercup.el
index 83e3c6e..d02bc07 100644
--- a/buttercup.el
+++ b/buttercup.el
@@ -384,6 +384,10 @@ A disabled spec is not run."
(setq new-value (lambda (&rest args)
(buttercup--spy-add-call new-value args)
(apply arg args))))
+ ((eq keyword :and-throw-error)
+ (setq new-value (lambda (&rest args)
+ (buttercup--spy-add-call new-value args)
+ (signal arg "Stubbed error"))))
((not keyword)
(setq new-value (lambda (&rest args)
(buttercup--spy-add-call new-value args)