Issues with JustMock and Large Test Runs
Ok so I have an issue with JustMock that I am hoping that someone here can help me with. I have a class with several test methods(something like 80). Each of these methods when run independently will pass with flying colors. However I have some methods that when run as part of a larger test run (say the whole class) will fail.
These test methods are mocking non public methods using JustMock mocking framework and then calling the public interface method that calls each of these private methods. Below is an example
///<summary>
/// A test for CheckCommError
///</summary>
[TestMethod]
public void CheckCommErrorWithErrorTest()
{
var message = new byte[] { (byte)EagleMessageTypes.SendCommError,BitConverter.GetBytes((ushort)10)[0], BitConverter.GetBytes((ushort)10)[1] };
bool expected = false; // TODO: Initialize to an appropriate value
bool actual;
//mock the other members in EagleUtility that are being used...
var target = new EagleUtility();
bool called = true;
Mock.NonPublic.Arrange<bool>(target, "CheckMinimumSize", ArgExpr.IsAny<BasicEagleMessage>(), Arg.AnyInt).IgnoreArguments().DoInstead(
(BasicEagleMessage arg1, int arg2) => called = true).Returns(true).MustBeCalled();
Mock.NonPublic.Arrange<bool>(target, "CheckMessageLength", Arg.AnyInt, ArgExpr.IsAny<BasicEagleMessage>()).IgnoreArguments().DoInstead(
(int arg1, BasicEagleMessage arg2) => called = true).Returns(true).MustBeCalled();
Mock.NonPublic.Arrange<bool>(target, "VerifyCheckSum", ArgExpr.IsAny<byte[]>()).IgnoreArguments().DoInstead(
(byte[] arg1) => called = true).Returns(true).MustBeCalled();
Mock.NonPublic.Arrange<bool>(target, "ValidateLegacyMessageType", ArgExpr.IsAny<BasicEagleMessage>()).IgnoreArguments().DoInstead(
(BasicEagleMessage arg1) => called = true).Returns(true).MustBeCalled();
actual = target.ValidateMessage(message, 3, Tower开发者_高级运维Types.Tower4800);
Assert.AreEqual(expected, actual);
Mock.Assert(target);
}
///<summary>
/// A test for CheckMessageLength
///</summary>
[TestMethod]
public void CheckMessageLengthTest()
{
var message = new byte[] { (byte)3, BitConverter.GetBytes((ushort)59006)[0], BitConverter.GetBytes((ushort)59006)[1], 0 };
var byteCount = 32486;
bool expected = true;
bool actual;
//mock the other members in EagleUtility that are being used...
var target = new EagleUtility();
bool called = true;
Mock.NonPublic.Arrange<bool>(target, "CheckMinimumSize", ArgExpr.IsAny<BasicEagleMessage>(), Arg.AnyInt).IgnoreArguments().DoInstead(
(BasicEagleMessage arg1, int arg2) => called = true).Returns(true).MustBeCalled();
Mock.NonPublic.Arrange<bool>(target, "CheckCommError", ArgExpr.IsAny<BasicEagleMessage>()).IgnoreArguments().DoInstead(
(BasicEagleMessage arg2) => called = true).Returns(true).MustBeCalled();
Mock.NonPublic.Arrange<bool>(target, "VerifyCheckSum", ArgExpr.IsAny<byte[]>()).IgnoreArguments().DoInstead(
(byte[] arg1) => called = true).Returns(true).MustBeCalled();
Mock.NonPublic.Arrange<bool>(target, "ValidateLegacyMessageType", ArgExpr.IsAny<BasicEagleMessage>()).IgnoreArguments().DoInstead(
(BasicEagleMessage arg1) => called = true).Returns(true).MustBeCalled();
actual = target.ValidateMessage(message, byteCount, TowerTypes.Tower4800);
Assert.AreEqual(expected, actual);
Mock.Assert(target);
}
When I run each of these individually they pass but when run as a set the first method passes and the second one fails. Depending on how I run the test (which test runner) the test method fails on different asserts, using mstest it fails on Asser.AreEqual, while using JustCode testrunner it fails on Mock.Assert (stating that methods marked MustBeCalled aren't being called).
I have Mock.Initialize(); in my class initializer function for the test class.
I appreciate any help and thank you all in advance.
Two links might help you:
- Telerik's answer
- MSTest test initialization behaviour
精彩评论