Bill Wake | 7 Jul 2011 16:47
Picon

Re: Duplicate constants

On Thu, Jul 7, 2011 at 7:17 AM, arnonaxelrod <arnonaxelrod <at> hotmail.com>wrote:

> **
> Here's another question I often have doubts about:
>
> Suppose I have to write a method that returns a string, which should be
> defined internally in the application (not entered by the user). So when
> I first write the test I write this string inside the test. For example:
>
> var result = MyMethod();
>
> Assert.AreEqual("This is a constant string", result);
>
> And then when I implement MyMethod() I write the exact same string
> inside that method. The problem with that is that the string is
> duplicated between the test and the production code.
>
Right. Usually I take that duplication as a sign that generalization will be
coming. I guess I don't have a lot of situations where constant strings are
the "secret" of this class. Ideally, those are pulled out somewhere.

> This problem is very common in tests that verify the messages of
> exceptions.
>
That I can say for sure - I almost never check the messages of an
exception.

>
> Another "twist" to this question is what happens if the string is
> composed with parameters (e.g. using string.Format). In this case the
> entire string.Format expression has to be duplicated.
>
 I can see this happening sometimes, but if there's a lot of it I'd expect
there's a missing abstraction somewhere.

> Is this kind of duplications between test and production code normal in
> TDD, or am I doing something wrong?
>
To me, there tends to be more of it in the beginning and less over time. You
might take a look at Kent Becks' book "Test-Driven Development". He talks
about this kind of duplication a bit, and his book will give you a good
sense of how TDD is done. (And let me make a commercial plug for Industrial
Logic's TDD materials at http://industriallogic.com/shop; we have some great
study material on TDD and refactoring.)

-- 
   Bill Wake
   Industrial Logic, Inc.  http://industriallogic.com  <at> IndustrialLogic
   Coaching | Training | Assessment | eLearning

[Non-text portions of this message have been removed]

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

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:
    testdrivendevelopment-digest <at> yahoogroups.com 
    testdrivendevelopment-fullfeatured <at> yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    testdrivendevelopment-unsubscribe <at> yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/


Gmane