Reuse stubs/redefs thoughout a context

68 views
Skip to first unread message

G Gordon Worley III

unread,
Apr 17, 2014, 2:41:47 PM4/17/14
to spe...@googlegroups.com
Cross posted from http://stackoverflow.com/questions/23140785/reusing-a-stub-redef-across-a-speclj-context

I'm writing tests for a Clojure app using Speclj. I'm accustomed in BDD to do things like this:

context "some context"
  stub my-function :return true
  it "has behavior one"
    should true my-function
  it "has behavior two"
    should_not false my-function

But in Speclj I can't seem to find an example of how to share the stub across the characteristics, so I'm currently stuck writing code like this:

(describe "this"
  (context "that"
    (it "accepts nil"
      (with-redefs [called-fn (constantly nil)]
          (should= nil (my-fn nil)))))
    (it "accepts 1"
      (with-redefs [called-fn (constantly nil)]
          (should= 100 (my-fn 1))))))

(I realize this is a somewhat contrived example and arguably those assertions could all go under one characteristic, but let's suppose for now that I have good reason to write the code like this.)

I want, however, to just have to stub called-fn once, but moving this up out of the its raises errors because the real called-fn gets called instead of my redef.

Is there a way to reuse redefs (or use Speclj stubs) in Speclj so I'm not stuck pushing them all down inside the characteristics?


Micah Martin

unread,
Apr 18, 2014, 10:56:38 AM4/18/14
to spe...@googlegroups.com
Gordan,
You can use the ‘around’ component to remove the duplication.  See below.
Micah
(describe “this & that”
  (around [it]
    (with-redefs [called-fn (constantly nil)]
      (it)))
  (it "accepts nil"
    (should= nil (my-fn nil)))
  (it "accepts 1"
    (should= 100 (my-fn 1))))


--
You received this message because you are subscribed to the Google Groups "speclj" group.
To unsubscribe from this group and stop receiving emails from it, send an email to speclj+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc
Reply all
Reply to author
Forward
0 new messages