Hi,
I was just comparing different mock frameworks to decide which one to use, when I found some very strange behavior. When I repeatedly run the same set of tests without changing anything in the source code, the result of the one test that uses TypeMock alternates between pass and fail.
I'm using VS 2002 with TestDriven.NET and .NET Framework SDK 1.0. Additionally, I installed .NET Framework 1.1 (just the framework, not the SDK) especially for TypeMock.
Since I was just comparing frameworks my source code looks like a very contrived example.
The class under test:
using System;
namespace Sandbox
{
public class Calculator
{
private IMath math;
public Calculator(IMath math)
{
this.math = math;
}
internal int Triple(int x)
{
return math.Multiply(x, 3);
}
}
}
The class to be mocked:
using System;
namespace Sandbox
{
public class CMath : IMath
{
public virtual int Multiply(int x1, int x2)
{
// make sure the returned result is false
if ((x1 == 0) || (x2 == 0))
{
return 99;
}
else
{
return 0;
}
}
}
}
And its interface:
using System;
namespace Sandbox
{
public interface IMath
{
int Multiply(int x1, int x2);
}
}
The Test Class:
using System;
using NUnit.Framework;
using TypeMock;
namespace Sandbox.UnitTest
{
[TestFixture]
public class TFTypeMock
{
[Test]
public void Triple()
{
// setup mock/dependencies
MockManager.Init();
Mock math = MockManager.Mock(typeof(CMath));
Calculator calculator = new Calculator(new CMath());
math.Strict = true;
// expectations
math.ExpectAndReturn("Multiply", 0).Args(0,3);
math.ExpectAndReturn("Multiply", 3).Args(1,3);
math.ExpectAndReturn("Multiply", 6).Args(2,3);
math.ExpectAndReturn("Multiply", 3000);
// execution/assertions
Assert.AreEqual(0, calculator.Triple(0));
Assert.AreEqual(3, calculator.Triple(1));
Assert.AreEqual(6, calculator.Triple(2));
Assert.AreEqual(3000, calculator.Triple(1000));
// verify mock
MockManager.Verify();
}
}
}
Output of a failing run:
TestCase 'Sandbox.UnitTest.TFTypeMock.Triple' failed:
expected:<0>
but was:<99>
c:mydocsisual studio projectssandbox ftypemock.cs(29,0): at Sandbox.UnitTest.TFTypeMock.Triple()