JustMock 和大型测试运行的问题
好的,我对 JustMock 有一个问题,希望有人能帮助我解决。 我有一堂课有几种测试方法(大约 80 个)。当独立运行时,这些方法中的每一个都会出色地通过。然而,我有一些方法,当作为更大的测试运行(比如整个类)的一部分运行时将会失败。
这些测试方法使用 JustMock 模拟框架模拟非公共方法,然后调用调用每个私有方法的公共接口方法。 下面是一个示例,
///<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, TowerTypes.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);
}
当我单独运行每个方法时,它们会通过,但是当作为一组运行时,第一个方法会通过,而第二个方法会失败。根据我运行测试的方式(哪个测试运行程序),测试方法在不同的断言上失败,使用 mstest 它在 Asser.AreEqual 上失败,而使用 JustCode testrunner 它在 Mock.Assert 上失败(说明标记为 MustBeCalled 的方法未被调用) )。
我有 Mock.Initialize();在我的测试类的类初始化函数中。
我感谢任何帮助,并提前感谢大家。
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, TowerTypes.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两个链接可能会帮助您:
Two links might help you: