c# - 单元测试纯领域驱动类

标签 c# unit-testing tdd nunit domain-driven-design

我的目标是创建一个基于纯领域驱动设计的系统。据我所知,这意味着我的域对象应该有行为但没有形状。也就是说,它们不应该有任何 getter 或其他访问器。

与此同时,我正在尝试遵循 TDD 流程,但在尝试编写测试时遇到了绊脚石。

[Test]
public class new_purchase_order_should_have_purchase_ordernumber_of_1
{
     PurchaseOrder po = PurchaseOrder.CreatePurchaseOrder()
     Assert.AreEqual(1,po.PurchaseOrderNumber); 
}

public class PurchaseOrder
{
       private int _purchaseOrderNumber;
       static CreatePurchaseOrder()
       {
           _purchaseOrderNumber = SomeWayOfGettingAPONumber()
           //other initialisation
       }

        public int PurchaseOrderNumber {get { return _purchaseOrderNumber;}
}

如果不允许使用 getter,我该如何验证 CreatePurchaseOrder() 方法是否正常运行并将值设置为 1。

在尝试实现此设计时,这对我来说是一个很大的概念障碍,因此任何建议都会非常有用。

谢谢

最佳答案

为什么域对象不能有属性?您所说的纯行为,它只是静态方法,它们与域对象无关。

wikipedia告诉我们:

a business object usually does nothing itself but holds a set of instance variables or properties, also known as attributes, and associations with other business objects, weaving a map of objects representing the business relationships.

A domain model where business objects do not have behavior is called an Anemic Domain Model.

因此,领域对象应该具有属性和(在大多数情况下)行为。

Martin Fowler说:

Domain Model - An object model of the domain that incorporates both behavior and data

关于c# - 单元测试纯领域驱动类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4985380/

相关文章:

c# - 查找是否多次包含相同元素的字符串列表

c# - 404 异常时更新 View

unit-testing - OpenCover 的运行时间比 nunit-console 长得多

c# - 当返回类型为 ActionResult 时,如何对操作进行单元测试?

reactjs - @testing-library/react TDD,react-bootstrap : Is this kind of test useful or just time loose

c# - 组织 WinForm 控件代码

c# - 双显示器的持久窗体位置和大小

java - 单元测试中未执行函数

java - Android 测试中的 OutOfMemoryError

c - 单元测试 : cohabitation of the production-code and the mocked implementation