像所有优秀的程序员一样,在将 TDD 与 MS Test 结合使用时,我试图弄清楚一些事情。我遵循基本的 Arrange、Act、Assert 模式,有些东西对于我的 Act 代码来说太复杂了。我假设 Act 行中应该只有一个 Action 。那么,鉴于我下面的示例代码,我是否通过先执行一个操作然后测试其条件而偏离轨道?感谢您的输入。
[TestMethod]
public void TheCountOfAllRecordsIsGreaterThanZero()
{
//Arrange
var auditLog = new AuditMasterClass();
//Act
//Create a new record in a local list of objects
auditLog.LogAction("MyPersonName", DateTime.Now, "Stuff", "MoreStuff",
"Desc",
"Comments", true, false,
"UndoStatement");
//Return the count of objects in the local list
var count = auditLog.GetCommentCount();
//Assert
Assert.IsTrue(count > 0);
}
最佳答案
测试对我来说似乎很好 - 我不会在这里太教条,但如果它让你感觉更好,你可以标记行:var count = auditLog.GetCommentCount();
作为一部分断言阶段 ;)
我要在测试中改变的一件事是实际断言 - 使用 Assert.AreNotEqual(0, count)
或 Assert.IsTrue(count > 0, string.Format("Count不大于 0,它是 {0}", count))
- 这样您将在断言失败时收到更好的错误消息。
关于c# - TDD 与 MS 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4079367/