Home > .NET, .NET User Groups, Test Driven Development > TDD Teaser #1 – Do you know the answer?

TDD Teaser #1 – Do you know the answer?

I recentlyconcluded a TDD with ASP.NET presentation in Cleveland with several TDD Teaser problems for the audience. The idea proved to be a lot of fun so I decided to repost the problems on my blog.

The Rules:

  1. Prize (a Thycotic keyring light) to be mailed to the firstcomment with the correct answer on this blog post withvalid contact information.
  2. My definition of the problem is the correct one. 🙂 Other answersmight be right butwon’t win the prize.
  3. I will post the answer within a few days.

What is wrong with the following code?

1using NUnit.Framework; 
2
3namespace UnitTests.BadCode
4{
5 [TestFixture]
6 public class Problem1
7 {
8 [Test]
9 public virtual void TestSomething()
10 {
11 MyClass a = new MyClass();
12 TestAmount(a);
13 }
14
15 public virtual void TestAmount(MyClass a)
16 {
17 Assert.AreEqual(2500, a.Amount);
18 }
19 }
20}

Winner: Roy Osherove

Answer & Discussion (click and drag your mouse to see the answer)

Kirk – you are right. It doesn’t compile the return type on TestAmount should have been void (Note to self: make sure your teaser compiles!). Unfortunately that wasn’t the one I was looking for. I have changed the return type to void as it should have been.

Dean – virtual methods and inheritance can be quite useful in tests – you should refactor your tests as judiciously as your production code and this may result in inherited tests. I often use inheritance when writing validity checks for different scenarios. I agree with your comments about TestAmount being poorly designed.

Answer:
NUnit, for backwards compatibility to 1.0, will identify TestAmount as a test due to the “Test” prefix on the method name. This results in an error since Tests may not have parameters. Why did NUnit 1.0 look for the Test prefix rather than use attributes? – because it was ported from JUnit which uses naming conventions since Java doesn’t have attributes.

Well done Roy! (your keyring light is on its way)

Thanks to everyone for participating! 🙂

  1. Kirk Marple
    October 7, 2004 at 12:50 am

    TestAmount is supposed to be returning a string but all it does is Assert. it shouldn’t even compile.

    Kirk
    kirk@agnostic-media.com

  2. http://
    October 7, 2004 at 1:43 am

    I can see two things at first glance:

    First is the fact that all methods are virtual. The class should be probably be sealed to start with – test cases are not meant to be extended in that manner.

    Second, you’ve separated your test case into two methods. What happens if you change one method and not the other? At the very least your TestAmount method should have an “expected” parameter which it uses instead of the hard-coded 2500. The TestSomething method can pass in the expected value when it calls TestAmount. What I’m trying to say here is that TestSomething knows what the state of “a” should be, but TestAmount does not necessarily know.

    dean.blogger@codeka.com

  3. Roy Osherove
    October 7, 2004 at 2:37 am

    TestAmount is public and begins with “Test”. because of backwards compatibility issues Nunit will run that method as well thinking it is a test. However, it accepts parameters which means it will won’t run and you’ll get a nice error.
    Right?

  4. Roy Osherove
    October 7, 2004 at 2:38 am

    oh yeah – Roy at osherove dot com is my email 🙂

  5. Roy Osherove
    October 7, 2004 at 2:41 am

    other ideas:
    if you *are* going to use a separate verification method, it should at least be named “VerifyXX” and be private. You also didint’ specify an error message on failure (the third missing parameter in the assert) and that’s usually a no-no.

  6. http://
    October 7, 2004 at 4:02 am

    Assert.AreEqual(2500, a.Amount) will never run since the method does not have the [Test] attribute?

  7. Matt Berther
    October 7, 2004 at 4:02 am

    Roy: I thought that that was the issue as well, but after thinking about it, I think that the backwards compatability with methods that begin with “Test” also requires the method to return void and accept no parameters.

  8. Haacked
    October 7, 2004 at 4:28 am

    Kirk is correct in that it won’t compile.

    Assuming you correct that by changing TestAmount to return void, you end up with two test methods in NUnit.

    One that correctly runs the assertion and one that displays the method TestAmount and says it must not have parameters.

    haacked@yahoo.com

  9. Roy Osherove
    October 7, 2004 at 9:16 am

    dang – it won’t compile.

  10. Samir
    April 8, 2007 at 2:26 am

    http://8620f5c96361d9bb2976aa017a6abddb-t.k49ols9.info 8620f5c96361d9bb2976aa017a6abddb [url]http://8620f5c96361d9bb2976aa017a6abddb-b1.k49ols9.info[/url] [url=http://8620f5c96361d9bb2976aa017a6abddb-b2.k49ols9.info]8620f5c96361d9bb2976aa017a6abddb[/url] [u]http://8620f5c96361d9bb2976aa017a6abddb-b3.k49ols9.info[/u] 00c1f002ef240e804156597842785ea4

  11. Hassan
    April 16, 2007 at 4:12 am

    http://076e49ab98b667676d0058829f382fb9-t.k90u0h.info 076e49ab98b667676d0058829f382fb9 [url]http://076e49ab98b667676d0058829f382fb9-b1.k90u0h.info[/url] [url=http://076e49ab98b667676d0058829f382fb9-b2.k90u0h.info]076e49ab98b667676d0058829f382fb9[/url] [u]http://076e49ab98b667676d0058829f382fb9-b3.k90u0h.info[/u] 86b4eaaaf8201e29470f084ce1d2d695

  12. Panayotis
    November 5, 2007 at 7:37 pm

    interesting

  13. Miltos
    November 8, 2007 at 7:39 pm

    interesting

  14. Marinos
    November 9, 2007 at 11:39 am

    Cool.

  15. Lambro
    November 9, 2007 at 2:19 pm

    Nice!

  16. Kleanthe
    November 12, 2007 at 11:03 pm

    Interesting…

  17. Zacharias
    November 13, 2007 at 12:14 pm

    Cool…

  18. Loukianos
    November 13, 2007 at 3:39 pm

    Nice!

  19. Stavros
    November 13, 2007 at 6:56 pm

    Nice…

  20. Apostolos
    November 13, 2007 at 7:32 pm

    Nice

  21. Philippos
    November 13, 2007 at 7:50 pm

    Cool…

  22. Petros
    November 13, 2007 at 11:28 pm

    Cool!

  23. Yannas
    November 14, 2007 at 1:03 am

    Interesting…

  24. Achilles
    November 14, 2007 at 6:58 am

    interesting

  25. Herakles
    November 14, 2007 at 7:27 am

    Cool.

  26. Spiridon
    November 15, 2007 at 3:42 pm

    Sorry 😦

  27. Ivan
    November 16, 2007 at 10:46 am

    Nice

  28. Ivan
    November 17, 2007 at 1:07 am

    Nice

  29. Spiro
    November 17, 2007 at 7:43 am

    Cool…

  30. Pantelis
    November 19, 2007 at 12:42 am

    Cool.

  31. Kalinikos
    November 20, 2007 at 9:04 am

    Cool…

  32. Metrophanes
    November 22, 2007 at 2:18 am

    Interesting…

  33. Ivan
    November 22, 2007 at 2:22 am

    Nice

  34. Cosmo
    November 24, 2007 at 1:30 am

    interesting

  35. Euaggelos
    December 1, 2007 at 10:45 am

    Cool.

  36. Charilaos
    December 9, 2007 at 7:51 pm

    Interesting…

  37. Ivan
    December 16, 2007 at 11:45 am

    interesting

  38. Ivan
    December 21, 2007 at 1:49 pm

    Nice

  39. Kristion
    December 21, 2007 at 4:42 pm

    interesting

  40. Ivan
    December 22, 2007 at 5:18 pm

    Nice

  41. Vaggelis
    January 2, 2008 at 8:15 am

    interesting

  42. Thrasyvoulos
    January 10, 2008 at 1:21 am

    interesting

  43. Makis
    January 21, 2008 at 2:21 pm

    Interesting…

  44. Stathis
    February 8, 2008 at 4:29 am

    Nice!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: