给定一个确保防止在 class 参数中错误分配值的单元测试,如何编写一个考虑到这一点的方法?
given a unit test that make sure that prevent wrong-assigned values in a class argument, how to write a method that takes that into account?
我最近了解到测试驱动开发。但是,我很难理解它。给出了以下单元测试:
public void Setup()
{
invoicePosition = new InvoicePosition{
Customer = new Customer(),
ItemIdentifier = 0,
ItemName = "SpringRoll",
Orders = 2,
SingleUnitPrice = 3.50m
};
pairs = new KeywordPair[]{
new KeywordPair(new Keyword("ItemNumber"),invoicePosition.ItemIdentifier.ToString()),
new KeywordPair(new Keyword("ItemName"), invoicePosition.ItemName),
new KeywordPair(new Keyword("CustomerName"), invoicePosition.Customer.Name),
new KeywordPair(new Keyword("AmountOrdered"), invoicePosition.Orders.ToString()),
new KeywordPair(new Keyword("NetPrice"), invoicePosition.SingleUnitPrice.ToString())
};
}
[Test]
public void Invoice_CreateOrderOrderedInput_Valid(){
var invoice = InvoicePosition.CreateFromPairs(pairs);
Assert.AreEqual(invoicePosition.ItemIdentifier, invoice.ItemIdentifier);
Assert.AreEqual(invoicePosition.ItemName.GetType(), invoice.ItemName.GetType());
Assert.AreEqual(invoicePosition.Customer.Name, invoice.Customer.Name);
Assert.AreEqual(invoicePosition.Orders, invoice.Orders);
Assert.AreEqual(invoicePosition.SingleUnitPrice, invoice.SingleUnitPrice);
}
请注意关键字和关键字结构如下:
public struct Keyword
{
private string keyword;
private KeywordTypes type;
public Keyword(string keyword, KeywordTypes Type = KeywordTypes.String){
this.keyword = keyword;
this.type = Type;
}
public struct KeywordPair
{
public Keyword Key;
public string Value;
public KeywordPair(Keyword key, string value)
{
this.Key = key;
this.Value = value;
}
}
现在我已经为之前的单元测试编写了以下项目:
命名空间 SimpleShop
public class InvoicePosition
{
public uint ItemIdentifier = 0;
public string ItemName = "";
public uint Orders = 0;
public decimal SingleUnitPrice = 0.0m;
public Customer Customer;
public KeywordPair[] Pairs = new KeywordPair[0];
public static InvoicePosition CreateFromPairs(KeywordPair[] pairs)
{
var invoice = new InvoicePosition();
invoice.Pairs = pairs;
return invoice;
}
}
我需要编写代码来解释错误的序列化。例如,如果将 pairs 引入到程序中会怎样:
pairs[3] = new KeywordPair(new Keyword("AmountOrdered"), "+%&/" + invoicePosition.Orders.ToString());
pairs[4] = new KeywordPair(new Keyword("NetPrice"), invoicePosition.SingleUnitPrice.ToString() +
"%&öä/");
谢谢,任何帮助都会很棒。
鉴于测试 Invoice_CreateOrderOrderedInput_Valid
,OP 中建议的 InvoicePosition
未通过测试 。通过使用 Visual Studio 的各种重构工具,像这样的东西是 可能可行的最简单的东西:
public class InvoicePosition
{
public Customer Customer { get; set; }
public int ItemIdentifier { get; set; }
public string ItemName { get; set; }
public int Orders { get; set; }
public decimal SingleUnitPrice { get; set; }
public static InvoicePosition CreateFromPairs(KeywordPair[] pairs)
{
return new InvoicePosition
{
ItemName = "",
Customer = new Customer(),
Orders = 2,
SingleUnitPrice = 3.5m
};
}
}
CreateFromPairs
实现显然不是完整或正确的实现,但它通过了所有测试。 TDD 的挑战是弄清楚接下来要编写哪个测试。
如果你能以尽可能小的步骤前进,TDD 效果最好。您可能要考虑 Transformation Priority Premise。您首先要解决错误实施的哪一部分?
也许您对 ItemName
被硬编码为空字符串感到不满。您如何编写一个新的测试来鼓励您使实现更通用?
我会编写另一个测试,使第一个测试中的所有内容保持不变,除了 ItemName
值。
测试的当前结构方式及其庞大的 Setup
方法使得这很不方便。我建议您改写 parametrised tests。
我最近了解到测试驱动开发。但是,我很难理解它。给出了以下单元测试:
public void Setup()
{
invoicePosition = new InvoicePosition{
Customer = new Customer(),
ItemIdentifier = 0,
ItemName = "SpringRoll",
Orders = 2,
SingleUnitPrice = 3.50m
};
pairs = new KeywordPair[]{
new KeywordPair(new Keyword("ItemNumber"),invoicePosition.ItemIdentifier.ToString()),
new KeywordPair(new Keyword("ItemName"), invoicePosition.ItemName),
new KeywordPair(new Keyword("CustomerName"), invoicePosition.Customer.Name),
new KeywordPair(new Keyword("AmountOrdered"), invoicePosition.Orders.ToString()),
new KeywordPair(new Keyword("NetPrice"), invoicePosition.SingleUnitPrice.ToString())
};
}
[Test]
public void Invoice_CreateOrderOrderedInput_Valid(){
var invoice = InvoicePosition.CreateFromPairs(pairs);
Assert.AreEqual(invoicePosition.ItemIdentifier, invoice.ItemIdentifier);
Assert.AreEqual(invoicePosition.ItemName.GetType(), invoice.ItemName.GetType());
Assert.AreEqual(invoicePosition.Customer.Name, invoice.Customer.Name);
Assert.AreEqual(invoicePosition.Orders, invoice.Orders);
Assert.AreEqual(invoicePosition.SingleUnitPrice, invoice.SingleUnitPrice);
}
请注意关键字和关键字结构如下:
public struct Keyword
{
private string keyword;
private KeywordTypes type;
public Keyword(string keyword, KeywordTypes Type = KeywordTypes.String){
this.keyword = keyword;
this.type = Type;
}
public struct KeywordPair
{
public Keyword Key;
public string Value;
public KeywordPair(Keyword key, string value)
{
this.Key = key;
this.Value = value;
}
}
现在我已经为之前的单元测试编写了以下项目: 命名空间 SimpleShop
public class InvoicePosition
{
public uint ItemIdentifier = 0;
public string ItemName = "";
public uint Orders = 0;
public decimal SingleUnitPrice = 0.0m;
public Customer Customer;
public KeywordPair[] Pairs = new KeywordPair[0];
public static InvoicePosition CreateFromPairs(KeywordPair[] pairs)
{
var invoice = new InvoicePosition();
invoice.Pairs = pairs;
return invoice;
}
}
我需要编写代码来解释错误的序列化。例如,如果将 pairs 引入到程序中会怎样:
pairs[3] = new KeywordPair(new Keyword("AmountOrdered"), "+%&/" + invoicePosition.Orders.ToString());
pairs[4] = new KeywordPair(new Keyword("NetPrice"), invoicePosition.SingleUnitPrice.ToString() +
"%&öä/");
谢谢,任何帮助都会很棒。
鉴于测试 Invoice_CreateOrderOrderedInput_Valid
,OP 中建议的 InvoicePosition
未通过测试 。通过使用 Visual Studio 的各种重构工具,像这样的东西是 可能可行的最简单的东西:
public class InvoicePosition
{
public Customer Customer { get; set; }
public int ItemIdentifier { get; set; }
public string ItemName { get; set; }
public int Orders { get; set; }
public decimal SingleUnitPrice { get; set; }
public static InvoicePosition CreateFromPairs(KeywordPair[] pairs)
{
return new InvoicePosition
{
ItemName = "",
Customer = new Customer(),
Orders = 2,
SingleUnitPrice = 3.5m
};
}
}
CreateFromPairs
实现显然不是完整或正确的实现,但它通过了所有测试。 TDD 的挑战是弄清楚接下来要编写哪个测试。
如果你能以尽可能小的步骤前进,TDD 效果最好。您可能要考虑 Transformation Priority Premise。您首先要解决错误实施的哪一部分?
也许您对 ItemName
被硬编码为空字符串感到不满。您如何编写一个新的测试来鼓励您使实现更通用?
我会编写另一个测试,使第一个测试中的所有内容保持不变,除了 ItemName
值。
测试的当前结构方式及其庞大的 Setup
方法使得这很不方便。我建议您改写 parametrised tests。