TDD和业务规则 - 网络API 2.0
想知道如果我这样做是正确的?TDD和业务规则 - 网络API 2.0
业务规则
-
Customer
可以不在系统中存在W/OProperty
。 - 当我们删除
Property
时,Customer
也被删除。 - 该只有时间我添加一个
Customer
是当我添加一个Property
。 (见下文)
的POST:JSON数据从客户端
// http://localhost:3541/api/property
{
City: "Demo City",
Customer: {Id: 0, Name: "New Customer", Email: "[email protected]"},
Id: 0,
Name: "Custom Property Name",
State: "LA",
StreetAddress1: "123 Main St.",
StreetAddress2: "Apt 1",
Type: {Id: 15, Name: "Developer"},
UserId: 174,
Zip: "12345"
}
的控制器
[Route("api/property")]
[HttpPost]
public async Task<int> AddProperty(Property property)
{
return await propertyManager.AddProperty(property);
}
属性管理器类
class PropertyManager{
PropertyManager(){
...
}
PropertyManager(PropertyRepo propertyRepo){
...
}
public int AddProperty(Property property){
int propId = await _propertyRepo.AddProperty(property);
// associate with customer
AddCustomer(property.Customer, propId); // here is my question (see below)
}
// again, here is my question (see below)
private int AddCustomer(Customer customer, int propId){
int custId = _propertyRepo.AddCustomer(customer, propId);
}
}
类财产
class Property{
public int Id {get; set;}
public string StreetAddress1 {get; set;}
...
public Customer Customer {get; set;}
}
在我的单元测试,我测试的业务逻辑的PropertyManager
类的,看看我是否能正确添加Property
。
因此,Customer
在该过程中创建。这必须在同一时间内完成 - 在同一个POST期间(如上面所描述的JSON)。现在
,我的问题是:
(或者我即将进行的方式是)
我测试的我添加Property
能力的业务逻辑。上述规则我永远不会添加Customer
不添加第一个Property
。我的单元测试因此是Property
centric。
我断言可以通过Property
和我的一天测试添加Customer
的能力。
正确?!?
或者你会建议在有一个强大的真实世界的时候重新设计系统来解耦到属性和客户这两个类的耦合,后者不能与前者一起存在。
TDD正在测试业务规则,所以说TDD与业务规则有些矛盾。
我的猜测是你担心在测试中测试多个事物,但是请记住,如果测试中逻辑相似,可以测试多个测试项目。因此,在您的业务规则是客户的情况下,必须在具有测试的属性旁边创建客户,这是完全正确的。
例如,您可能有类似的这些测试:
GivenAPropertyWhenACustomerDoesNotExistThenExceptionThrown
GivenAPropertyWhenPropertyValidAndCustomerValidThenPropertySuccessfullyAdded
然后你就断言在两个属性和客户
0检查边评论
你不必一定重新设计你的代码,但它似乎有趣,当创建一个属性,因此,如果一个人有多个属性,他们会在你的数据库多次客户创造的呢?由于我不了解您的要求或业务,所以我的看法是正确的,但确实看起来很独特。
还记得TDD是关于首先编写测试,如果你已经编写了你的代码,现在正在编写测试,这不是TDD。
HTH