c# - TDD 与 MS 测试

标签 c# tdd

像所有优秀的程序员一样,在将 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/

相关文章:

c# - 为什么我要使用 HashSet 而不是 Dictionary?

c# - Max()函数的测试用例设计

unit-testing - BDD主要用于集成测试吗?

c# - 预加载多对多 - EF Core

testing - 是否可以使用 filecheck 来匹配 CMAKE 项目中另一个文件的值?

c# - 二维对象数组返回类型 - NSubstitute

ruby-on-rails - Controller Spec#Show Action : exoected: [Object], 得到:无错误 - RSpec 3

c# - Mondor 的 MSCaptcha 在部署到托管服务时不会加载图像

c# - 更新动态控件

c# - 绑定(bind)、线程和 winforms