Hi Alex,

Unfortunately the some-fn has to be created at the top level; it has
to be a var to be dynamically bindable, and Midje (and AFAIK, all
other mocking frameworks) use dynamic binding.

Here's what I came up with, using clojure.contrib.mock.test-adapter:

(deftest test-undo-patch!-calls-undo-fn
  (def mock-fn +)
  (def mock-undo-fn -)
  (expect [mock-fn (times once (has-args [1 2]))
           mock-undo-fn (times once (has-args []))]
    (defmethod undo-fn mock-fn [patch]
      mock-undo-fn)
    (let [patch (do-patch! mock-fn 1 2)]
      (undo-patch! patch))
    (remove-method undo-fn mock-fn))
  (ns-unmap *ns* 'mock-fn)
  (ns-unmap *ns* 'mock-undo-fn))

Note that the multimethod cannot be mocked, so that we have to use
defmethod/remove-method.  Extending the mock frameworks to support
this shouldn't be difficult.

The var for dynamic binding issue is a bigger deal.  Does anyone know
if all statements in the deftest are guaranteed to be executed, in
case a statement in the middle throws an exception?  Otherwise, the
namespace may not be cleaned properly.

Thanks,
Alyssa

On Dec 22, 1:26 am, Alex Baranosky <alexander.barano...@gmail.com>
wrote:
> So I lied, I couldn't resist doing just one more:
>
> (defn some-fn [] nil)
>
> (fact "calls the anonymous function that undo-fn returns"
>   (undo-patch ...patch...) => @patches-
>   (provided
>     (undo-fn ...patch...) => some-fn
>     (some-fn) => nil))
>
> The two provided statements are mboth mocking and stubbing.  So if either of
> those functions isn't called with the expected arguments they will fail.
>  The line
> (undo-patch ...patch...) => @patches- is saying when I call undo-patch with
> any patch, it should return the value inside of patches-  This is probably
> ok, since the different undo-fns are thoroughly unit tested.
>
> There might be some better way to not have to create that some-fn that we
> just use for testing.  Maybe put it in a let or something?
>
> Best,
> Alex
>
> P.S. Midje can be found on Github and Clojars.https://github.com/marick/Midje

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to