I am trying to learn TDD by applying it to my simple project. Some details (and an earlier question) are here:
TDD: Testable Class Writing Assistance
Specificity: I have a PurchaseOrderCollection class that has a private PurchaseOrders list (passed in the constructor), and BuyOrders has the IsValid boolean property. PurchaseOrderCollection has a HasErrors property that returns true if any of the BuyOrders in the list has IsValid as false. This is the logic I want to test.
[TestMethod] public void Purchase_Order_Collection_Has_Errors_Is_True_If_Any_Purchase_Order_Has_Is_Valid_False() { List<PurchaseOrder> orders = new List<PurchaseOrder>(); orders.Add(new PurchaseOrder(--some values to generate IsValid false--)); orders.Add(new PurchaseOrder(--some values to generate IsValid true--)); PurchaseOrderCollection collection = new PurchaseOrderCollection(orders); Assert.IsTrue(collection.HasErrors); }
This is similar to my previous question that this test is too related to the fact that I need to know the logic of what makes PurchaseOrder IsValid false or true in order to pass the test when really this test should not care. The question is different (imo) in that the classes themselves are not a problem.
Essentially, I want to be able to declare a PurchaseOrder with IsValid false or true, without knowing anything more about what PurchaseOrder is.
From my limited knowledge of TDD, this is what you use Stubs or Mocks for. My main question is, is that right? Or should I use a different method for this? Or am I completely messed up and am I just writing this test and thinking about it wrong?
My initial thought was to just use some kind of fake structure and create a PurchaseOrder that always returns true or false. From what I read, I needed to declare IsValid virtual. So my second thought was to modify it to add iPurchaseOrder as an interface for PurchaseOrder and just create a fake PurchaseOrder that always returns false or true. Are there both of these valid ideas?
Thanks!
source share