Hi,
I found that ExpectAndReturn may throw exception if used in conjunction with VerifyMock and having the expected method being called twice and written on the same line. Here's the sample code.
First, define two classes, namely SizeDT and epsilonT
public class SizeDT
{
private double x;
private double y;
public SizeDT(double xx, double yy)
{
x = xx;
y = yy;
}
public bool IsEmpty
{
get
{
bool xEp = epsilonT.IsEpsilon(this.x);
bool yEp = epsilonT.IsEpsilon(this.y);
if ( xEp&& yEp)
return true;
return false;
}
}
public bool IsEmpty2
{
get
{
if ( epsilonT.IsEpsilon(this.x)&& epsilonT.IsEpsilon(this.y))
return true;
return false;
}
}
}
And
public class epsilonT
{
public epsilonT()
{
}
public static bool IsEpsilon(double ptValue)
{
return ptValue==0;
}
}
Now, run the following test class
using System;
using NUnit.Framework;
using TypeMock;
namespace ClassLibrary1
{
[TestFixture]
[ClearMocks]
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
[SetUp]
public void Init()
{
}
[TearDown]
public void TearDown()
{
}
[Test]
[VerifyMocks]
public void CallDiffLines()
{
Mock mock = MockManager.Mock(typeof(epsilonT));
mock.ExpectAndReturn("IsEpsilon", false);
mock.ExpectAndReturn("IsEpsilon", false);
SizeDT size = new SizeDT(0,0);
Assert.IsFalse(size.IsEmpty);
}
[Test]
[VerifyMocks]
public void CallSameLines()
{
Mock mock = MockManager.Mock(typeof(epsilonT));
mock.ExpectAndReturn("IsEpsilon", false);
mock.ExpectAndReturn("IsEpsilon", false);
SizeDT size = new SizeDT(0,0);
Assert.IsFalse(size.IsEmpty2);
}
}
}
The CallSameLines method will return the following exception, but the CallDiffLines won't:
------ Test started: Assembly: ClassLibrary1.dll ------
TestCase 'ClassLibrary1.Class1.CallSameLines'
failed: TypeMock.VerifyException :
TypeMock Verification: Method ClassLibrary1.epsilonT.IsEpsilon() has 1 more expected calls
at TypeMock.MockManager.Verify()
at TypeMock.VerifyMocksAttribute.Execute()
at TypeMock.DecoratorAttribute.CallDecoratedMethod()
at TypeMock.ClearMocksAttribute.Execute()
at TypeMock.MethodDecorator.d()
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
c:documents and settingssoon hui.esteemsoftmy documentsjobs hird party components ypemockclasslibrary1class1.cs(78,0): at ClassLibrary1.Class1.CallSameLines()
________
Lincoln-Zephyr V12 engine history