clojure-spec

About: http://clojure.org/about/spec Guide: http://clojure.org/guides/spec API: https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html
PB 2019-12-13T20:32:44.002500Z

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 "<http://not-realurl.com>")]
    (is (= "<http://not-realurl.com>"
           (sut/send-email2 "String is ignored because of the redef")))))

 (abc-test) =&gt; nil
The test using defspec:
(defspec send-email-test 1
  (with-redefs [sut/gen-uri (constantly "<http://not-realurl.com>")]
    (prop/for-all [domain (s/gen string?)]
                  (is (= "<http://not-realurl.com>"
                         (sut/send-email2 domain))))))

(send-email-test) =&gt;

FAIL in () (email_test.clj:24)
expected: "<http://not-realurl.com>"
  actual: ("<https://mydomain.com/v3//messages>")
{:result false,
 :seed 1576269139182,
 :failing-size 0,
 :num-tests 1,
 :fail [""],
 :shrunk {:total-nodes-visited 0, :depth 0, :result false, :smallest [""]}}

2019-12-13T20:42:48.003100Z

Not to override generators like that, no

2019-12-13T20:43:22.004Z

I'm assuming gen-uri is a generator

PB 2019-12-13T20:44:02.004600Z

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

(defn gen-uri
  [domain route]
  (-&gt; domain
      base-url
      (str route)))

PB 2019-12-13T20:44:19.004900Z

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

PB 2019-12-13T20:44:43.005500Z

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

2019-12-13T20:45:05.006100Z

Then try reversing the order of prop and with-redefs

PB 2019-12-13T20:45:38.006300Z

Oh I feel silly

PB 2019-12-13T20:45:46.006500Z

I didn't expect that to throw things off

PB 2019-12-13T20:46:07.006700Z

Thanky ou

2019-12-13T20:47:03.007700Z

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

2019-12-13T20:47:15.008Z

Similar to if you put it around a defn

PB 2019-12-13T20:47:42.008300Z

That makes sense.

PB 2019-12-13T20:47:56.008500Z

Thank you again!

👍 1
alexmiller 2019-12-13T21:40:19.009Z

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