2 places I wish I could use TypeMock – part 2
As I showed in the previous post here are the other places where the TypeMock team would have loved to use TypeMock Case 2. Mocking another Process. The Tracer is a class that is used to send and recieve trace messages to the Tracer GUI. Before the tracer pushes a message into the message pipe, it checks if a GUI exists.
private bool ClientIsRunning()
{
// Code that checks that a Tracer GUI is up and runnning, using inter-process mutex
return processFound;
}
In order to test the Tracer we have to fake that the client is running, without having the real GUI. We cannot set the mutex as Windows will not block mutexes in the same process.
Using TypeMock we would do the following:
| Mock m = MockManager.Mock(typeof(Tracer)); m.AlwaysReturn(“ClientIsRunning”,true); |
Simple, but we cannot so we did the following:
We changed the signiture of ClientIsRunning to:
| protected virtual bool ClientIsRunning |
and created a Mocked Tracer:
public class TestTracerMock : Tracer
{
protected override bool ClientIsRunning()
{
return true;
}
}
And used a technique to swap the real Tracer with our TestTracerMock. Here is how:
Our tracer is a singleton so that it can be used throught the framework:
internal static Tracer Instance
{
get
{
if (tracer==null)
{
tracer = new Tracer();
}
return tracer;
}
}
We swap the Tracer by inserting the MockedTracer into the tracer field:
[TestFixtureSetUp]
public void ChangeTracer()
{
TestTracerMock traceMock = new TestTracerMock();
typeof(TypeMock.Tracer).GetField(“tracer”,
BindingFlags.NonPublic|BindingFlags.Static)
.SetValue(null,traceMock);
}
We had to change the Tracers constructor to public too.
2 Comments to “2 places I wish I could use TypeMock – part 2”
Recent Posts
- Top 5 questions to ask when checking references
- Unacceptable: Unit testing will take 20 years to catch on
- The 4 reasons why we DIDN’T choose Oslo
- Typemock Academy Launch
- The First Rule to Software Craftsmanship
Categories
- .NET Tests
- Agile
- Code Integrity
- Community
- Debugging
- Fun
- Management for Geeks
- Marketing
- Product
- Release
- Reviews
- SharePoint
- TDD
- Time Management
- Uncategorized
- Unit Tests
Archives
- August 2010
- May 2010
- April 2010
- March 2010
- February 2010
- December 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- December 2008
- November 2008
- August 2008
- July 2008
- May 2008
- April 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006

The Author, you – super hero!
http://unikont.com
Well, thanks!
Good aftenoon !
http://assolt.com
Very nicely done forum.
Thanks much!