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) => 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) =>
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 [""]}}
Not to override generators like that, no
I'm assuming gen-uri is a generator
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)))
I see how it was poorly named for this example, sorry for that
What I really want to mock is the http call to the service that sends the email. BUt wanted to keep things simple
Then try reversing the order of prop and with-redefs
Oh I feel silly
I didn't expect that to throw things off
Thanky ou
The prop call is essentially creating a function that gets called later, at which point your with-redefs has already exited
Similar to if you put it around a defn
That makes sense.
Thank you again!
I don't know if you've looked at it, but clojure.spec.test.alpha/instrument has the ability to mock and stub