Fork me on GitHub
#clojure-spec
<
2019-12-13
>
PB20:12:44

Is it not possible to use with-redefs inside of defspec? I have a relatively simple test that seems to suggest that it's not working: The function I'm testing is here:

(defn send-email2
  [domain]
  (gen-uri domain "/messages"))
The test using deftest:
(deftest abc-test
  (with-redefs [sut/gen-uri (constantly "")]
    (is (= ""
           (sut/send-email2 "String is ignored because of the redef")))))

 (abc-test) => nil
The test using defspec:
(defspec send-email-test 1
  (with-redefs [sut/gen-uri (constantly "")]
    (prop/for-all [domain (s/gen string?)]
                  (is (= ""
                         (sut/send-email2 domain))))))

(send-email-test) =>

FAIL in () (email_test.clj:24)
expected: ""
  actual: ("")
{:result false,
 :seed 1576269139182,
 :failing-size 0,
 :num-tests 1,
 :fail [""],
 :shrunk {:total-nodes-visited 0, :depth 0, :result false, :smallest [""]}}

gfredericks20:12:48

Not to override generators like that, no

gfredericks20:12:22

I'm assuming gen-uri is a generator

PB20:12:02

gen-uri is a function that generates a url to a service I'm using to send email:

(defn gen-uri
  [domain route]
  (-> domain
      base-url
      (str route)))

PB20:12:19

I see how it was poorly named for this example, sorry for that

PB20:12:43

What I really want to mock is the http call to the service that sends the email. BUt wanted to keep things simple

gfredericks20:12:05

Then try reversing the order of prop and with-redefs

PB20:12:38

Oh I feel silly

PB20:12:46

I didn't expect that to throw things off

PB20:12:07

Thanky ou

gfredericks20:12:03

The prop call is essentially creating a function that gets called later, at which point your with-redefs has already exited

gfredericks20:12:15

Similar to if you put it around a defn

PB20:12:42

That makes sense.

PB20:12:56

Thank you again!

👍 4
Alex Miller (Clojure team)21:12:19

I don't know if you've looked at it, but clojure.spec.test.alpha/instrument has the ability to mock and stub