Re: Statics and caches and mocks oh my!

Michal Svoboda <[email protected]>
Newsgroups gmane.comp.programming.test-driven-development
Message-ID <[email protected]>
David Burstin wrote:
> One option is to add static methods to the repositories to inject canned
> data into their internal lists for testing, but that seems wrong to me - it
> exposes their internal implementation and breaks encapsulation (is there
> such a thing with static objects?).

This is a "pattern" from the book "working with legacy code" - it's good
because it's (relatively) non-invasive and allows you to put your legacy
code under test for further refactoring. So good but temporary solution.

If you're afraid the setters will be used from normal code you can guard
by a boolean that a test must set first.

> Another is to create an interface and have the production code use a
> concrete implementation that internally calls the static methods.In testing
> I can inject mocks. Again, this seems messy to me.

Can you substitute the database itself and let your test data be cached
as they normally would? It's always better to "mock" as little as
possible to avoid tests diverging from reality.

> I personally don't like the use of static objects, but I don't know what a
> better alternative is that would provide long-lived caching and easy
> accessibility from anywhere in the code.

Caches don't need to be static, they just need to be reachable enough so
that they can cache something. Accessibility from anywhere in the code
is actually a design smell I think.

> Everyone else on the team thinks that they are a great solution.

I hear you. It's not easy to convince others of their design problems
unless there is hard factual evidence. Look around the user code of that
object and you'll find the smells.

Michal Svoboda



------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/testdrivendevelopment/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/testdrivendevelopment/join
    (Yahoo! ID required)

<*> To change settings via email:
    [email protected] 
    [email protected]

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.