circleci / bond

spying for tests
127 stars 28 forks source link

James Bond learns how to set traps. #9

Closed marcomorain closed 3 years ago

marcomorain commented 8 years ago

Add a new macro, with-trap that throws and exception is any of the supplied functions are called.

The rational for addign this macro is to avoid this common pattern when testing Clojure:

(bond/with-stub [foo]
  (bar)
  (is (zero? (foo bond/calls count))))

This can be replaced with the following:

(bond/with-trap [foo]
  (bar)

If foo is not called by bar in this example, then the code will behave as normal and the result of bar will be returned from the bond/with-trap form. If foo is called, then bond/with-trap will throw an exception.

gordonsyme commented 8 years ago

I like the idea but not sure exceptions are the best way to implement it. Generally fns that are stubbed are at least one call away from the test code so it's not hard to imagine a case where intermediate code catches the Exception raised by with-trap.

An assert throws an Error which is less likely to be intercepted. Using a failing is from clojure.test would rule out other testing frameworks :(

marcomorain commented 8 years ago

Yeah, I'm not sure how best to implement this either (and @cayennes does not like this approach).

Generally fns that are stubbed are at least one call away from the test code so it's not hard to imagine a case where intermediate code catches the Exception raised by with-trap.

It's the call to with-trap that throws, not the stubbed function, so I don't think this is the case.

gordonsyme commented 8 years ago

It's the call to with-trap that throws, not the stubbed function, so I don't think this is the case.

Ah cool, my bad, carry on :)

neeasade commented 3 years ago

Closing this as a stale PR -- if there is still interest, we can rebase against the default branch or open an issue to discuss it.