How to test methods that call other methods in a domain object

When working with domain objects, how do you usually use the unit test method, which calls another method in the object? For example:

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;
  }
}

Testing GetTotalAmt () modules is easy, but with GetExtendedTotalAmt () I have to use InvoiceLine stubs / layouts to work when all I really want to do is check that the discount applies if the IsDiscounted flag is true.

How do other people deal with this? I don’t think it makes sense to split the domain object, as these methods are considered part of the main function of the invoice (and splitting is likely to force developers to turn to the wrong method more often).

Thanks!

+5
2

: InvoiceLine 1.

- :

invoice.Add(new InvoiceLine(new Article("blah", 1M), 1));

Assert.AreEqual(0.95M, invoice.GetExtendedTotalAmt());

, , .., , ( - - ). , , , .

+1

GetTotalAmt virtual, :

var sut = new MockRepository().PartialMock<Invoice>();
sut.Expect(x => x.GetTotalAmt()).Return(10);
sut.Replay();

var result = sut.GetExtendedTotalAmt();
+3

All Articles