I have a similar issue to http://www.telerik.com/community/forums/justmock/general-discussions/mocks-failing-to-work-correctly-in-large-test-runs.aspx
I have the following code:
ClassInitialize Method
TestMethod
Method Being Tested.
When I run the RecieveIncomingMessageTest method on it's own everything passes as it should. However when I run it as part of the larger test run that test method fails.
I am sure it is because I have a test method that tests the actual funtionality of ProcessIncomingMessage in the same test class( because they are methods in the same concrete class).
How can I mock the ProcessIncomingMessage method for the RecieveIncomingMessageTest method but not for the ProcessIncomingMessageTest method so they run and pass as part of the larger test run that occurs?
Thanks again.
I have the following code:
ClassInitialize Method
[ClassInitialize()]
public
static
void
MyClassInitialize(TestContext testContext)
{
Mock.Partial<EagleUtility>().For(() => EagleUtility.ValidateMessage(Arg.IsAny<byte[]>(), Arg.AnyInt, TowerTypes.Unified)); Mock.Partial<UnifiedProtocolTranslator>();
}
TestMethod
public
void
RecieveIncomingMessageTest()
{
var expectedTower = TestContext.DataRow[
"Tower"
].ToString();
var expectedEventDescription = TestContext.DataRow[
"EventDescription"
].ToString().TrimStart(
'\r'
,
'\n'
,
' '
);
expectedEventDescription = expectedEventDescription.TrimEnd(
' '
,
'\n'
,
'\r'
,
'\0'
);
var rawDataToUse = Convert.FromBase64String(TestContext.DataRow[
"RawData"
].ToString());
var called =
false
;
var target =
new
UnifiedProtocolTranslator();
;
int
byteCount = rawDataToUse.Length;
EagleIncomingMessageStatus expected = EagleIncomingMessageStatus.Complete;
EagleIncomingMessageStatus actual;
Mock.NonPublic.Arrange<bool>(target, "ProcessIncomingMessage", rawDataToUse, 0).IgnoreArguments().DoInstead((byte[] arg1,int arg2) => called =true).Returns(true).MustBeCalled();
Mock.NonPublic.Arrange(target,
"CompileIncomingMessage"
).DoNothing().MustBeCalled();
actual = target.RecieveIncomingMessage(rawDataToUse, byteCount);
Mock.Assert(target);
Assert.AreEqual(expected, actual);
}
Method Being Tested.
public
EagleIncomingMessageStatus RecieveIncomingMessage(
byte
[] message,
int
byteCount)
{
var messageType = (UnifiedIncomingMessageTypes) message[0];
EagleIncomingMessageStatus returnValue = EagleIncomingMessageStatus.Continue;
if
(EagleUtility.ValidateMessage(message, byteCount, TowerTypes.Unified))
{
if
(ProcessIncomingMessage(message, byteCount))
{
CompileIncomingMessage();
returnValue = EagleIncomingMessageStatus.Complete;
}
}
return
returnValue;
}
When I run the RecieveIncomingMessageTest method on it's own everything passes as it should. However when I run it as part of the larger test run that test method fails.
I am sure it is because I have a test method that tests the actual funtionality of ProcessIncomingMessage in the same test class( because they are methods in the same concrete class).
How can I mock the ProcessIncomingMessage method for the RecieveIncomingMessageTest method but not for the ProcessIncomingMessageTest method so they run and pass as part of the larger test run that occurs?
Thanks again.