TDD Teaser #2 - Think you know NUnit?
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 running the following Test?
1using NUnit.Framework;
2
3namespace UnitTests.BadCode
4{
5 [TestFixture]
6 public abstract class Problem2
7 {
8 [Test]
9 public virtual void TestSomething()
10 {
11 RedHerring @int = new RedHerring();
12 Assert.AreEqual(1, @int.BogusNumber);
13 }
14 }
15 public class RedHerring
16 {
17 public int BogusNumber;
18 }
19}
UPDATE: I am looking specifically for an error rather than a test failure.
Winner: Kirk MarpleAnswer & Discussion (click and drag your mouse to see the answer)
Duncan - you are right. The value of BogusNumber would initialize to zero as it is an instance variable. I was looking for an error though not a test failure. Answer: NUnit creates an instance of the TestFixture labeled classes when it runs and calls their Test labeled methods. It cannot create an instance of an abstract class so it will ignore it in the NUnit GUI (Problem2 won't even appear). However using the NUnitAddIn and selecting to run the individual Test will cause the error that the instance can't be instantiated. Abstract classes are very useful when refactoring your test suites. We get bitten by this little quirk all the time on our projects when an inherited test fail in the unit test report and the first thing we do is try to run the individual test on our workstations. Well done to Kirk for the first correct answer. Well done also to Ron Buckton and Vishal Narayan Saxena for elaborating on the answer! |