TDD Teaser #1 - Do you know the answer?
I recently concluded 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:
- Prize (a Thycotic keyring light) to be mailed to the first comment with the correct answer on this blog post with valid contact information.
- My definition of the problem is the correct one. :) Other answers might be right but won't win the prize.
- I will post the answer within a few days.
What is wrong with the following code?
1using NUnit.Framework;Winner: Roy Osherove
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}
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! :-) |