Archive for August 11th, 2008
-
Fake Objects
I was writing some tests today around some legacy code (its about 2 weeks old and not written with TDD) and wanted to make sure that some method was called when the parent method was invoked. Unfortunately, I was having some problems with getting RhinoMocks working. Since I was getting frustrated with some “stupid” tool and not making any progress, I decided to go back to first principles to look for a simpler way to write my test.
This was the code I was working with and I wanted to write a test that proved line 28 was called:
22 public virtual void AcceptMessage(IBusMessage message) 23 { 24 if (!Power) 25 { 26 return; 27 } 28 ProcessMessage(message); 29 }If you look at the rest of the code, this method comes from an abstract base class and the method in question (line 28) is abstract, protected AND void, so just calling it and looking at the return value is not a strategy I can use. To write the other tests I had written earlier in the day I had used the Fake Object concept just so I can get the object in the testing harness. Keep in mind, I am not trying to fake any collaborator, just test the base class since it does real things I want to make sure happen (or get updated) as the design evolves (you might be thinking this is a dumb place to start with a test, but you have to start somewhere).
Since I have complete control over my fake object and the method I want to make sure is abstract, I can insert a sensing variable in my fake object and set its state when the method (ProcessMethod) is called, like so:
10 public bool MessageProcessed = false; 11 12 protected override void ProcessMessage(IBusMessage message) 13 { 14 MessageProcessed = true; 15 }Here is my test:
63 [TestMethod] 64 public void AcceptMessageWithPowerTest() 65 { 66 // setup 67 FakeDevice test = new FakeDevice(); 68 test.Power = true; 69 70 IBusMessage message = new BusMessage(); 71 72 // exercise 73 test.AcceptMessage(message); 74 75 // verify 76 Assert.IsTrue(test.MessageProcessed, "Process message not called."); 77 78 // clean-up 79 }OK, kinda like using a sledgehammer to hit a carpet nail, but it got me unstuck and back to a green bar. Now, maybe I can spend sometime with RhinoMocks and figure out the easier way to do this.
Frequent Topics
Agile Agile SD Certified ScrumMaster Coaching Collaboration Communication Conferences Daily Scrum Design Excellence Design for Six Sigma Extreme Programming Games Innovation Games Lean Legacy Code Links of the Week Measures Movies Pair Programming Personal Planning PMI Practices Presentations Product Owner Quality Refactoring Retrospectives Rugby Scrum ScrumMaster SIMSOC Spain Team Test-Driven Development Testing Tools Training Transitions Travel Voice of the Customer
Calendar
Recent Comments
- Kenneth van Rumste on Scrum Roles Defined
- Carlton on Scrum Roles Defined
- Kenneth van Rumste on Scrum Roles Defined
User Groups
Archives
- May 2011 (1)
- February 2011 (1)
- January 2011 (5)
- November 2010 (3)
- October 2010 (6)
- September 2010 (5)
- August 2010 (4)
- July 2010 (6)
- June 2010 (2)
- May 2010 (2)
- April 2010 (5)
- March 2010 (3)
- February 2010 (5)
- January 2010 (7)
- December 2009 (8)
- November 2009 (2)
- October 2009 (6)
- September 2009 (9)
- August 2009 (7)
- July 2009 (4)
- June 2009 (3)
- May 2009 (3)
- April 2009 (5)
- March 2009 (6)
- February 2009 (6)
- January 2009 (7)
- December 2008 (10)
- November 2008 (11)
- October 2008 (10)
- September 2008 (4)
- August 2008 (5)
- July 2008 (5)
- June 2008 (8)
- May 2008 (5)
- April 2008 (3)
