On Fri, Sep 16, 2011 at 4:28 PM, Chris Perkins <chrisperkin...@gmail.com>wrote:

> On Friday, September 16, 2011 3:12:49 PM UTC-4, Brian Hurt wrote:
>>
>>
>>
>> On Thu, Sep 15, 2011 at 6:42 AM, Chris Perkins <chrispe...@gmail.com>wrote:
>>
>>> On Wednesday, September 14, 2011 11:19:13 AM UTC-4, Brian Hurt wrote:
>>>>
>>>> Say I have two name spaces, A and B, with A depending on B.  I want to
>>>> test namespace A, replacing module B with a mock B for testing purposes-
>>>> preferably without having to load B at all (B sucks in a bunch of stuff,
>>>> like dependencies on databases and external web sites and etc. that I don't
>>>> want to deal with in testing).  What is the easy, clojure-approved,
>>>> mechanism for doing this?  I tried:
>>>>
>>>> How *should* I structure this code for testing?  I was assuming the
>> natural way to do this is to make A, B, and C separate name spaces but maybe
>> this is wrong.
>>
>> The problem isn't just *writing* the code- I need to be able to change
>> this code later, and have some assurance it still works.  So "load it up
>> into a repl and play with it" isn't a viable solution.
>>
>> Or is clojure code just not testable/maintainable?
>>
>> Brian
>>
>>
>  I don't have a detailed answer because I haven't done much mocking myself,
> but I'm confident that it's possible. The reason for my confidence is that
> references from one namespace to another live in Vars, which are mutable.
>
> Here's a very simple example: I made a namespace "a" containing a function
> "a-fn", which just calls b-fn in namespace b. Then I created a "mock-b" that
> defines a different b-fn.
>
> user=> (require 'a)
> nil
> user=> (a/a-fn)
> "real b"
> user=> (require 'mock-b)
> nil
> user=> (alter-var-root #'b/b-fn (constantly mock-b/b-fn))
> #<mock_b$b_fn mock_b$b_fn@1e1be92>
> user=> (a/a-fn)
> "mock b"
>
> Note that the behavior of a-fn has been altered at runtime.  I can imagine
> a mocking utility that, given a namespace (eg: "b"), and a mock namespace
> (eg: "mock-b"), would go through the vars in mock-b and call alter-var-root
> on all the matching vars in b before calling a test suite. In fact, I
> wouldn't be surprised at all to find this on github somewhere.
>
> Not much detail there, but I hope this helps somewhat.
>
>
>
For the record, this is exactly what I'm doing- I'm just kind of surprised I
have to actually create this infrastructure.

Brian

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to