我是Visual Studio单元测试框架的新手。我在XUnit中稍微涉足过一些问题(DUnit具体而言)。
我不知道为什么以下测试失败了。基于我的C#代码(展览A),我认为我的测试(展览B)会以众所周知的绚丽色彩传递。
[EXHIBIT A - 相关代码]
public class MessageClass
{
private int _messageTypeCode = 0;
private int _messageTypeSubcode;
private int _messageSequenceNumber;
private string _messageText;
public MessageClass()
{
this._messageTypeCode = 0;
this._messageTypeSubcode = 0;
this._messageSequenceNumber = 0;
this._messageText = string.Empty;
}
public void SetMessageTypeSubcode(int AMessageTypeSubcode)
{
int iMsgTypeSubCode = AMessageTypeSubcode;
if (iMsgTypeSubCode > 9999)
{
iMsgTypeSubCode = 9999;
}
else if (iMsgTypeSubCode < 0)
{
iMsgTypeSubCode = 42;
}
_messageTypeSubcode = AMessageTypeSubcode;
}
public int MessageTypeSubcode()
{
return _messageTypeSubcode;
}
[EXHIBIT B - 相应MessageClassTest中的测试代码]
[TestMethod()]
public void SetMessageTypeSubcodeTest()
{
int AMessageTypeSubcode;
// Should I put this class instantiation in MyTestInitialize?
MessageClass target = new MessageClass();
// Test 1
AMessageTypeSubcode = 0;
target.SetMessageTypeSubcode(AMessageTypeSubcode);
Assert.AreEqual(AMessageTypeSubcode, target.MessageTypeSubcode());
// Test 2 - 10000 is too much
AMessageTypeSubcode = 12345;
target.SetMessageTypeSubcode(AMessageTypeSubcode);
Assert.AreEqual(9999, target.MessageTypeSubcode());
// Test 3 - val must be positive
AMessageTypeSubcode = -77;
target.SetMessageTypeSubcode(AMessageTypeSubcode);
Assert.AreEqual(42, target.MessageTypeSubcode());
}
......第二次测试失败了。将val设置为高于截止值(9999)后,应将其指定为(9999)而不是12345。
正如我所说,我是Visual Studio单元测试框架的新手;在TestMethod中不可能有多个测试吗?或者我需要做一些事情,如调用flush()或finish()或close()或reset()或其他什么?
答案 0 :(得分:1)
测试失败,因为测试应该失败。你的方法不正确:
_messageTypeSubcode = AMessageTypeSubcode;
应该是:
_messageTypeSubcode = iMsgTypeSubCode ;