Showing posts with label unit-testing. Show all posts
Showing posts with label unit-testing. Show all posts

NUnitForms rocks!

This NunitForms tool is really cool. Kudos to Luke Maxon and co. I'm running v2.0 alpha 5 and so far, my favourite feature has got to be ControlTester.FireEvent(). For instance, I can simulate the drag operation with something like this:


// initialize control tester for our Finder control
ControlTester myControl = new ControlTester("picFinder", "MyTestForm");

// simulate drag to coordinate (105,205)
myControl.FireEvent("MouseDown", (EventArgs)null);
Cursor.Position = new Point(105, 205); // drag mouse to here
myControl.FireEvent("MouseMove", (EventArgs)null);
myControl.FireEvent("MouseUp", (EventArgs)null);

// Assert MyTestForm GUI states
...


Pretty neat.

Rhino.Mocks ExpectationViolationException

I'm trying out Rhino.Mocks in my NUnit test suites. Why Rhino.Mocks instead of NMock, you ask? Well, because I've heard that Rhino.Mocks can mock classes as well as interfaces.

Anyway, I was running into a little bit of a rookie's setback. I set up my mock objects like this:


1 const string TESTNAME = @"My Component Name";
2 MockRepository mocks = new MockRepository();
3 MyLib.MyTestClass tw = mocks.CreateMock<MyLib.MyTestClass>();
4 Expect.Call(tw.get_Name()).Return(TESTNAME);
5 mocks.ReplayAll();
// Test first invocation of get_Name()
6 Assert.AreEqual(tw.get_Name(), TESTNAME);
// Test second invocation of get_Name()
7 Assert.AreEqual(tw.get_Name(), TESTNAME);



I should probably add that MyLib.MyTestClass is a COM Interop class.

Executing line 7 resulted in this exception: Rhino.Mocks.Exceptions.ExpectationViolationException : _MyTestClass.get_Name(); Expected #1, Actual #2.

A little bit of googling did not immediately hint at the proper solution for me, but looking at the API documentation turned up this method: Repeat.AtLeastOnce(). So adjusting line 4 above to this did the trick:


4 Expect.Call(tw.get_Name()).Return(TESTNAME).Repeat.AtLeastOnce();