|
9:07:20 AM |
|
Started contributing to the .Net MockObjects project this weekend. It's still in the early stages but the fundamentals are there. I can't take any credit for that, Griffin has gotten off to a great start. All I've really done so far is convert the unit tests to NUnit 2.0. Watch this space though. [Pushing the envelope]
Great! This is something high up on my 'I need one of those' list at the moment. I'll be watching this quite closely.
I was dabbling a bit with a mock library for .NET using the System.Reflection.Emit stuff based on the concepts used in EasyMock and Nat Pryces' Mock Library. The advantage the Emit stuff has over Java dynamic proxies is it can be used to generate any code in memory (not just interface implementations).
Something like:
public class Thing { public int DoSomething(string arg) { //.. } } public class UsesThing { public int Go(Thing thing) { return thing.DoSomething("hello"); } } public class UsesThingTest { [Test] public void CheckCorrectArgumentPassed() { // setup mock Mock mockThing = new Mock(typeof(Thing)); mockThing.expectAndReturn("Go", 77, "hello"); // execute code to test UsesThing usesThing = new UsesThing(); int result = usesThing.Go((Thing)mockThing.Value); // check results Assertion.AssertEquals(77, result); mockThing.verify(); } }
Note that Mock is a generic class - internally it uses Emit stuff to extend Thing and override all the methods with mocked versions.
The only thing I'm stuck on is the fact that you cannot override methods that aren't marked as virtual.... hmmmm.......
|