Closed philomates closed 6 years ago
You've probably seen this page: https://github.com/marick/Midje/wiki/Prerequisites-and-protocols
If not, it's relevant.
Thanks for the pointer; I had forgotten about that doc.
Strangely, if I update the tests above to use defrecord-openly
they still fail in the same way.
I still haven't gotten a chance to dive into this or understand the constraints and ideas behind defrecord-openly
. That said, I would expected this to work, regardless of being a defrecord
or defrecord-openly
, because I'm not mocking a protocol method but rather a function called within the method.
It turns out that the issue I posted isn't specific to records. It has more to do with binding variables to new variables before the redef logic has a chance to kick in.
For example, the following code also exhibits the same issue:
(defn hof [f]
(fn [x] (f x)))
(defn my-incr [x] (+ 1 x))
(def obfuscated-incr (hof my-incr))
(fact "fails to pass because `my-incr` gets bound to `f` in `hof`"
(obfuscated-incr 1) => 20
(provided (my-incr 1) => 20))
(fact "passes because `my-incr` gets redefed"
((hof my-incr) 1) => 20
(provided (my-incr 1) => 20))
As far as I can tell, creating logic to allow for mocking in these contexts would be quite complicated (control-flow static analysis or memory-level manipulations) and hence not worth it given the gains.
Mocking functions that are called within record instances seems to be very temperamental
For instance, given the following definitions:
The following tests that define the record instance inside of the
fact
pass:But if we define the record instance outside of the
fact
theprovided
/against-background
overrides no longer work, causing the tests to faileFailing tests on a dev branch: https://github.com/phillipm/Midje/blob/7388e48c9a78d8fea37e13867d8e7f7193612312/test/as_documentation/about_defrecord/using_refer__provided_tests/test.clj#L42-L50