TDD:帮助与编写可测试类 [英] TDD: Help with writing Testable Class

查看:143
本文介绍了TDD:帮助与编写可测试类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小巧的应用程序,并想给一个尝试使用TDD发展。我从来没有使用TDD,实际上根本不知道那是什么,直到我发现ASP.NET MVC的。 (我的第一个MVC应用程序有单元测试,但他们是脆,再加方式,拿了太多了保留,并放弃了 - 我是来学习的单元测试TDD =!)

I have a quick little application and wanted to give a try to develop using TDD. I've never used TDD and actually didn't even know what it was until I found ASP.NET-MVC. (My first MVC app had unit tests, but they were brittle, way coupled, took too much up keep, and were abandoned -- I've come to learn unit tests != TDD).

在应用背景:

我有一个采购订单的文本转储读取为字符串。我需要分析文本,并返回新的采购订单号,新的行项目数,老采购订单号,老采购订单行号。 pretty简单。

I have a text dump of a purchase order read in as a string. I need to parse the text and return new purchase order number, new line item number, old purchase order number, old purchase order line number. Pretty simple.

现在,我唯一的工作新的采购订单的详细信息(数/线)和有这样一个模型:

Right now I'm only working on new purchase order details (number/line) and have a model like this:

public class PurchaseOrder
{
    public string NewNumber {get; private set;}
    public string NewLine {get; private set;}

    new public PurchaseOrder(string purchaseOrderText)
    {
        NewNumber = GetNewNumber(purchaseOrderText);
        NewLine = GetNewLine(purchaseOrderText);
    }

    // ... definition of GetNewNumber / GetNewLine ...
    //  both return null if they can't parse the text
}

现在我想补充的方法的IsValid如果NewNumber和换行都是非空只应是真实的。所以我想对它进行测试,如:

Now I want to add a method "IsValid" that should only be true if "NewNumber" and "NewLine" are both non-null. So I want to test it like:

public void Purchase_Order_Is_Valid_When_New_Purchase_Order_Number_And_Line_Number_Are_Not_Null()
{
    PurchaseOrder order = new PurchaseOrder()
    {
        NewNumber = "123456",
        NewLine = "001"
    };

    Assert.IsTrue(order.IsValid);
}

这是很容易的,但它似乎像一个坏的妥协,允许公共setter和参数的构造函数。因此,另一种方法是在构造一个purchaseOrderText的价值养活,但后来我测试code为'GetNewNumber'和'GetNewLine为好。

This is easy enough, but it seems like a bad compromise to allow public setters and a parameterless constructor. So the alternative is to feed in a 'purchaseOrderText' value in the constructor, but then I'm testing the code for 'GetNewNumber' and 'GetNewLine' as well.

我有点难倒就如何在试图保持它在情理之中的事情了模型项锁定了写这是一个可测试的类。这似乎是这将是一个共同的问题,所以我想我只是缺少一个明显的概念。

I'm kind of stumped on how to write this as a testable class while trying to keep it locked up in terms of what makes sense for the model. This seems like it would be a common problem, so I'm thinking I'm just missing an obvious concept.

推荐答案

解决方案之一是没有构造函数做的工作:

One solution is to not have the constructor do the work:

public class PurchaseOrder
{
    public PurchaseOrder(string newNumber, string newLine)
    {
        NewNumber = newNumber;
        NewLine = newLine;
    }
    // ...
}

然后测试是很简单的孤立 - 你不测试 GetNewNumber GetNewLine 同时

要帮助使用的PurchaseOrder 您可以创建一个把它在一起的工厂方法:

To help using PurchaseOrder you can create a factory method that puts it together:

public static PurchaseOrder CreatePurchaseOrder(string purchaseOrderText)
{
   return new PurchaseOrder(
     GetNewNumber(purchaseOrderText),
     GetNewLine(purchaseOrderText));
}

这篇关于TDD:帮助与编写可测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆