In my last post, I showed how to fake a dependency. But this involved doing a couple of things. First, it involved changing the original method. Then, I had to write wrappers (a real wrapper and the fake wrapper) and finally, I had to inject the fake object in the test. This is a lot of work, even for a very simplistic example. The solution for that is an isolation framework. Isolation frameworks isolate the code from their dependencies, but they do it programmatically, and without hassle. What makes Typemock Isolator 2010 different than other frameworks (like Rhino Mocks and Moq) is that you don’t need to change the tested code just to test it. Let’s look at the test we got last time – using a fake Message Queue to test that the message was called:[TestMethod]
public void Send_StringMessage_MessageWasSent()
{
var fakeQueue = new FakeMessageQueue(); var server = new Server();
server.SendMessage(fakeQueue, "message"); Assert.IsTrue(fakeQueue.messageWasSent);
}We’re actually doing two things in this test with our fakeMessageQueue object. The first is changing behavior – the SendMessage method does not call the original SendMessage method. The second is testing that the method was called. We do this by using a public field in the fake object. Read more: IUpdateable from Eric Nelson
public void Send_StringMessage_MessageWasSent()
{
var fakeQueue = new FakeMessageQueue(); var server = new Server();
server.SendMessage(fakeQueue, "message"); Assert.IsTrue(fakeQueue.messageWasSent);
}We’re actually doing two things in this test with our fakeMessageQueue object. The first is changing behavior – the SendMessage method does not call the original SendMessage method. The second is testing that the method was called. We do this by using a public field in the fake object. Read more: IUpdateable from Eric Nelson