TDD与MS测试

mic*_*hik 3 c# tdd

像所有优秀的程序员一样,在使用带有MS Test的TDD时,我正试图直截了当.我遵循基本的安排,行为,断言模式,对于我的行为代码看起来太复杂了.我假设法案行中只应该有一个动作.那么,鉴于我的示例代码如下,我是否首先执行一个操作然后测试其状态?感谢您的投入.

    [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);
    }
Run Code Online (Sandbox Code Playgroud)

Dro*_*per 5

测试对我来说似乎很好 - 我不会在这里太教条,但如果它让你感觉更好,你可以标记这一行: var count = auditLog.GetCommentCount();作为断言阶段的一部分;)

我将在测试中改变的一件事是实际断言 - 使用Assert.AreNotEqual(0, count)Assert.IsTrue(count > 0, string.Format("Count was not greater than 0, it was {0}", count))- 这样,如果断言失败,您将获得更好的错误消息.


Car*_*ter 5

我对你所做的事情没有任何疑问.我倾向于count简单地内联变量

Assert.IsTrue(auditLog.GetCommentCount() > 0);
Run Code Online (Sandbox Code Playgroud)

但它并没有太大的不同.正如所写的那样,你的测试表明你期望在使用一组特定参数调用LogAction()时,日志中会有一个或多个注释.这很清楚.我喜欢做的一件事是在行动之前断言我的断言,所以我知道行动真的是导致这种情况的原因.那当然是:

Assert.IsTrue(auditLog.GetCommentCount() == 0);
Run Code Online (Sandbox Code Playgroud)

就在你的法案之前.