Hi,
So I got a working test and it looks like this:
(facts "sets a value for the given key in redis"
(with-redefs [redis/with-conn (constantly "Ok")]
(require '[com.leonardoborges.cache :as cache] :reload)
(fact "with no ttl "
(cache/set "key" "value") => "Ok"
(provided
(redis/set "key" "value") => irrelevant))))
Note the 'require ... :reload' function call - it doesn't work without
that line.
My theory behind why this works is that when I reload the cache
namespace it recompiles it - and at that point, the new var binding is
available.
Is this the case? More importantly, is this how I should be doing this?
The source for cache/set is the same as before.
Cheers,
Leonardo Borges
www.leonardoborges.com
On Mon, Feb 18, 2013 at 10:07 AM, Leonardo Borges
<[email protected]> wrote:
> Hi,
>
> I have some caching code that relies on a macro called 'with-redis' so
> while writing my tests I'd like to redef this macro to bypass all of
> it's redis connection machinery and just execute a function that does
> nothing instead.
>
> Something like this works:
>
> (with-redefs [cache/with-redis (fn [& body] (prn "body called"))]
> (cache/with-redis (do "stuff")))
> ;; "body called"
>
> But this doesn't:
> (with-redefs [cache/with-redis (fn [& body] (prn "body called"))]
> (cache/set "key" "value"))
>
> It actually attempts to talk to redis and doesn't run the dummy function at
> all.
>
> For reference, cache/set is defined as follows, in my cache namespace:
>
> (defn set [key value]
> (with-redis
> (let [key (cache-key key)]
> (redis/set key value))))
>
>
> It would seem that 'with-redefs' doesn't work if the var your
> 'redefining' is being used in the body of a function you're calling?
>
> Or hopefully I'm just doing something wrong.
>
>
> Has anyone seen this before?
>
> Thanks,
> Leonardo Borges
> www.leonardoborges.com
Leonardo Borges
www.leonardoborges.com
On Mon, Feb 18, 2013 at 10:07 AM, Leonardo Borges
<[email protected]> wrote:
> Hi,
>
> I have some caching code that relies on a macro called 'with-redis' so
> while writing my tests I'd like to redef this macro to bypass all of
> it's redis connection machinery and just execute a function that does
> nothing instead.
>
> Something like this works:
>
> (with-redefs [cache/with-redis (fn [& body] (prn "body called"))]
> (cache/with-redis (do "stuff")))
> ;; "body called"
>
> But this doesn't:
> (with-redefs [cache/with-redis (fn [& body] (prn "body called"))]
> (cache/set "key" "value"))
>
> It actually attempts to talk to redis and doesn't run the dummy function at
> all.
>
> For reference, cache/set is defined as follows, in my cache namespace:
>
> (defn set [key value]
> (with-redis
> (let [key (cache-key key)]
> (redis/set key value))))
>
>
> It would seem that 'with-redefs' doesn't work if the var your
> 'redefining' is being used in the body of a function you're calling?
>
> Or hopefully I'm just doing something wrong.
>
>
> Has anyone seen this before?
>
> Thanks,
> Leonardo Borges
> www.leonardoborges.com
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.