Hello, I'd appreciate some advice on what's going wrong with my code (or expectations) here. I'm looking to introduce TypeMock into our development process, but am having some difficulties with the code below which is a cut-down version of our Production code.
What appears to be happening is that if I use a Isolate.WhenCalled() method with WithExactArguments() AFTER a previous Isolate.WhenCalled(), it seems to wipe out the expected value of the previous call.
In my example below I have two test cases. The first test case arranges that TransactionStatus will return EnumTranStatus.Validated. Then it arranges that the GetField() method with the parameter "Spread" returns a field object. However once the second WhenCalled() method is called, the TransactionStatus returns back to the default value.
In the second test case, I re-arrange the calls and it works as expected.
What is going wrong? I'd appreciate any help.
Here is the main test fixture (for the VS.NET test environment).
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TypeMock.ArrangeActAssert;
using TypeMock.Test.Code;
namespace TypeMock.Test
{
[TestClass]
public class TestFixture
{
private Field _spreadField;
[TestInitialize]
public void Initialise()
{
_spreadField = Isolate.Fake.Instance<Field>();
}
[TestMethod]
public void WithExactArgumentsAfterFails()
{
var transactionInfo = Isolate.Fake.Instance<IPreprocessingInfo>();
Isolate.WhenCalled(() => transactionInfo.Transaction.TransactionStatus).WillReturn(EnumTranStatus.Validated);
// transactionInfo.Transaction.TransactionStatus now returns Validated as expected.
Isolate.WhenCalled(() => transactionInfo.Transaction.GetField("Spread")).WithExactArguments().WillReturn(_spreadField);
// Why is transactionInfo.Transaction.TransactionStatus now being set as the default of "None"?
Assert.AreEqual(EnumTranStatus.Validated, transactionInfo.Transaction.TransactionStatus);
}
[TestMethod]
public void WithExactArgumentsBeforeSucceeds()
{
var transactionInfo = Isolate.Fake.Instance<IPreprocessingInfo>();
Isolate.WhenCalled(() => transactionInfo.Transaction.GetField("Spread")).WithExactArguments().WillReturn(_spreadField);
Isolate.WhenCalled(() => transactionInfo.Transaction.TransactionStatus).WillReturn(EnumTranStatus.Validated);
// This behaves as expected
Assert.AreEqual(EnumTranStatus.Validated, transactionInfo.Transaction.TransactionStatus);
}
}
}
And here are the associated types:
Field:
namespace TypeMock.Test.Code
{
public class Field
{
}
}
IPreprocessingInfo:
namespace TypeMock.Test.Code
{
public interface IPreprocessingInfo
{
Transaction Transaction { get; }
}
}
Transaction:
namespace TypeMock.Test.Code
{
public class Transaction
{
public Field GetField(string name)
{
return new Field();
}
public EnumTranStatus TransactionStatus { get; set; }
}
public enum EnumTranStatus
{
None = 0,
Validated = 1
}
}
This happens in 6.0.6 and 6.1.2.