使用域對象時,通常如何測試調用對象中另一個方法的方法?例如:如何測試調用域對象中其他方法的方法
public class Invoice
{
public IList<InvoiceLine> InvoiceLines;
public string Company;
public bool IsDiscounted;
public DateTime InvoiceDate;
//...
public GetTotalAmt();
public GetExtendedTotalAmt();
public decimal GetTotalAmt()
{
decimal total;
foreach (InvoiceLine il in InvoiceLines)
{
total += il.Qty * il.Price;
}
return total;
}
public decimal GetExtendedTotalAmt()
{
decimal discount;
if (IsDiscounted)
discount = .05M;
return GetTotalAmt() * discount;
}
}
單元測試GetTotalAmt()很容易,但GetExtendedTotalAmt()我不得不使用存根/模擬InvoiceLine對象,使其工作,當我真正想要做的是測試一個如果IsDiscounted標誌爲真,則應用折扣。
其他人怎麼處理?我認爲將域對象拆分是不合理的,因爲這些方法都被視爲核心發票功能的一部分(並且拆分它可能會導致開發人員更頻繁地調用錯誤的方法)。
謝謝!
那是犀牛嘲笑嗎? – 2010-01-28 09:09:20
是的,犀牛嘲笑,對不起,我應該提到這一點。 – 2010-01-28 09:10:41
這是一種非常干擾的方式,因爲很多方法需要虛擬化,類沒有密封等。 – 2010-01-28 09:27:13