• Stars
    star
    126
  • Rank 284,543 (Top 6 %)
  • Language
    Clojure
  • Created over 12 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

spying for tests

Bond CircleCI Status

Bond is a spying and stubbing library, primarily intended for tests.

[circleci/bond "0.6.0"]
(ns test.foo
  (:require [bond.james :as bond :refer [with-spy]]))

(defn foo [x]
  (let [shaken (with-out-str (prn :martini))]
    [shaken])

(defn bar [y]
  (foo y))

(deftest foo-is-called
  (with-spy [foo]
    (bar 2)
    (is (= 1 (-> foo bond/calls count)))))

Bond provides one main macro, with-spy. It takes a vector of defn vars (vars that resolve to fns). Each var will be redefined for the scope of the macro, wrapping the function to track arguments and call counts. At any point during the scope, you can call (bond/calls f), where f is a spied fn. calls returns a seq of maps, one for each call to f. Each map contains the keys :args, a seq of args the fn was called with, and one of :return or :throw.

Bond also provides with-stub!. It works the same as with-spy, but redefines the function to return (constantly nil) (default), while also spying on it. This is generally preferable to Clojure's built-in with-redefs macro since it will throw an exception if the mocked function is called with the wrong number of arguments. You can specify an arbitrary function instead of the default (constantly nil) by providing a [fn-var replacement-fn] vector in place of just the fn name:

(ns test.foo
  (:require [bond.james :as bond :refer [with-stub!]]))

(defn foo [x] ...)

(defn bar [y] ...)

(deftest foo-is-called
  (with-stub! [[foo (fn [x] "foo")]
               [bar (fn [y] "bar")]]
    (is (= ["foo" "bar"] [(foo 1) (bar 2)]))))

(deftest consecutive-stubbing
  (with-stub! [[foo [(fn [x] "foo1")
                     (fn [x] "foo2")
                     (fn [x] "foo3")]]
               [bar (fn [y] "bar")]]
    (is (= ["foo1" "foo2" "foo3" "bar"] [(foo 1) (foo 1) (foo 1) (bar 2)]))))

Private functions can also be stubbed or spyed:

(ns test.foo)

(defn- foo [x] ...)
(ns test.bar
  (:require [bond.james :as bond :refer [with-stub!]]
            [test.foo :as foo]))

(deftest foo-is-called
  (with-stub! [[foo/foo (fn [x] "foo")]]
    (is (= "foo" (#'foo/foo 1)))
    (is (= [1] (-> #'foo/foo bond/calls first :args)))))

There is also a with-stub macro which works like with-stub! but omits the argument check.

In addition to with-spy and with-stub!, Bond also provides with-spy-ns and with-stub-ns which can spy/stub every function in a namespace in one go:

(ns test.foo
  (:require [bond.james :as bond]
            [clojure.test :refer (deftest is)]))

(defn foo [] :foo)

(defn bar [] :bar)

(deftest you-can-stub-entire-ns
  (is (= :foo (foo)))
  (is (= :bar (bar)))
  (bond/with-stub-ns [[foo (constantly :baz)]]
    (is (= :baz (foo)))
    (is (= :baz (bar)))))

Releasing

New git tags are automatically published to clojars.

The following should be updated on the main branch before tagging:

  • project.clj - version
  • README.md - dependency coordinates
  • CHANGELOG.md - summary of changes

License

Distributed under the Eclipse Public License.

More Repositories

1

circleci-docs

Documentation for CircleCI.
JavaScript
760
star
2

circleci-images

Scripts to generate images for building projects on CircleCI 2.0
Shell
440
star
3

encrypted-files

Storing encrypted files in source on CircleCI
102
star
4

circleci.test

Enhanced Clojure test runner for tests written with clojure.test
Clojure
86
star
5

EspressoSample

A sample Android application with Espresso UI tests
Java
55
star
6

rollcage

A Clojure client for Rollbar
Clojure
50
star
7

CircleCI-Training-Koans

A set of puzzles to be completed to help familiarize yourself with CircleCI
Shell
38
star
8

clj-v8

Clojure wrapper for the v8 JS engine
C++
33
star
9

realitycheck

A sample app that reality-checks some basic CircleCI features
29
star
10

clj-keyczar

small, simple clojure wrapper for keyczar
25
star
11

analytics-clj

Idiomatic Clojure wrapper for the Segment.io 2.x Java client
Clojure
22
star
12

ex

Common Go packages for the execution teams
Go
22
star
13

docker

Fork: Docker to work in CircleCI unprivileged containers
Go
18
star
14

mongofinil

A Mongoid-like model library for Clojure and MongoDB
Clojure
17
star
15

lein-jarbin

Clojure
14
star
16

compojure-appengine-sample

Sample Compojure app with deployment to Google App Engine
Clojure
13
star
17

circle-env

Shell
11
star
18

lunch-n-learn

9
star
19

code2040-practice

A repository for practicing CI and CD
Python
8
star
20

android-cloud-test-lab

Example project on how to use Cloud Test Lab to test an Android app.
Java
8
star
21

media

Media files for marketing and integration projects
7
star
22

circleci-diag

Bash script to run diag commands in a build
Shell
5
star
23

android-sdk

Docker image for building Android projects
Dockerfile
5
star
24

circleci.com-shared

JavaScript
3
star
25

lein-deps-plus

Clojure
3
star
26

ex-service-template

Service template for projects using ex
Go
3
star
27

front-end-engineering-exercise

A short exercise to help us talk about ui development.
2
star
28

checkout-integration

2
star
29

git-demo

This project is a beginner git project
1
star
30

checkout-test

A repository used to test checkouts in build-agent. This copy is the original.
1
star
31

dp-test

1
star
32

fs

Clojure
1
star
33

circle-dummy-branch-repo

used for testing.
1
star
34

test-pm-repo-public

1
star
35

checkout-test-public

1
star