Well, don't let us be distracted anymore. Our next test is awaiting us: Test #2.
#
1a
1b
2
3
4
Get on with it and ...
We could do that from scratch, but let's do it efficient (sounds more professional than easy-going, or just plain lazy [:P]).
We already have our first test function PIwithOneLine in codeunit 60000 (Test Doc. Amount) and our second test function will have a lot in common and thus we will create a PIwithTwoLines test function as a copy of PIwithOneLine (you might recall one of my first blog entries on how to copy functions in NAV). Let's just show the code now:
PIwithTwoLines()
// Create a purchase headerPurchHeader.INSERT(TRUE);
// Create two purchase lines to the headerPurchLine."Document No." := PurchHeader."No.";PurchLine."Line Amount" := 1;PurchLine.INSERT;
PurchLine."Line Amount" := 2;PurchLine.INSERT;
//Check if line amounts equals the line amounts calculated by CalcDocAmountAssert.AreEqual(3,PurchHeader.CalcDocAmount,'Calc. Doc. Amount')
Minimal test code; no fuzz with variables to sum up the line amounts, hard coding suffices here.
... which shows GREEN!
Wow, this saves me to have to ...
... and hence we can ...
Indeed, RED:
Did I hear you saying: "You could have known, Luc!"? And right you are: I could have known, but I just wanted to demonstrate that the whole setup is capable of showing me the way. Of course I cannot insert two times the same purchase line, i.e. two purchase lines having the same PK.
As such the code should have been:
// Create two purchase lines to the headerPurchLine."Document No." := PurchHeader."No.";PurchLine."Line No." := 10000;PurchLine."Line Amount" := 1;PurchLine.INSERT;
PurchLine."Line No." := 20000;PurchLine."Line Amount" := 2;PurchLine.INSERT;
Let's update and rerun:
RED!
Makes sense? To me it does. The total of the lines is 3 (= 1 + 2), but my CalcDocAmount function (do you recall?) returns the "Line Amount"of the first line only. Here we are again to ...
Open table 38 in design mode and modify the CalcDocAmount function:
CalcDocAmount() Amount : Decimal
PurchLine.SETRANGE("Document No.","No."); IF PurchLine.FINDFIRST THEN REPEAT TotLineAmount := TotLineAmount + PurchLine."Line Amount"; UNTIL PurchLine.NEXT = 0;
EXIT(TotLineAmount)
Compile and ready to ...
(BTW: did I already say that I did declare a variable TotLineAmount of data type Decimal?)
... see it pass ... [8] ... RED/GREEN ... RED/GREEN ... [8][8] ... see it pass? ... [8][8][8] ... RED/GREEN ...
Ready? Run codeunit 60000:
YES ... GREEN! ... [Y] ... [{][}]
Refactor? Anything to refactor? Looking at both the production and test code ...?
CalcDocAmount seems quite OK now. What about our test functions? Have a look at this pattern:
PurchLine."Document No." := PurchHeader."No.";PurchLine."Line Amount" := ...;...PurchLine.INSERT;
Looks like we're hitting a duplication (or should we call it a triplication?). RE-FACT-OR! RE-FACT-OR! RE-FACT-OR! RE-FACT-OR! Can you hear the roaring drums?
OK, we'll refactor. Now relax, we will. [A]
What about this?
PIwithOneLine()
// Create a purchase headerPurchHeader.INSERT(TRUE); //TRUE ensures that the system assigns a unique number to the header
// Create one purchase line to the headerCreatePurchLine(PurchHeader."No.",0,1);
//Check if line amount equals the line amount calculated by CalcDocAmountAssert.AreEqual(1,PurchHeader.CalcDocAmount,'Calc. Doc. Amount')
// Create two purchase lines to the headerCreatePurchLine(PurchHeader."No.",10000,1);CreatePurchLine(PurchHeader."No.",20000,2);
With a new function (having PurchLine as local variable referring to table 39):
CreatePurchLine(HeaderNo : Code[20];LineNo : Integer;Amount : Decimal)
PurchLine."Document No." := HeaderNo;PurchLine."Line No." := LineNo;PurchLine."Line Amount" := Amount;PurchLine.INSERT;
Now ...
... which shows RED. RED? Yes, RED.
[:O]
Never seen that one before. Did you? But I guess that I know what this is about. Did I tell you that any function that we define in a test codeunit by default is of type (FunctionType) Test? Well, that's exactly what happened with our helper function CreatePurchLine:
And Test functions are not meant to be called to in code like Normal functions. So we change FunctionType to Normal, and also set Local to Yes.
Compile ... GREEN ... and ...
... pass! GREEN! Test #2 is completed:
... that, given previous results of our tests and therefor knowing that our production code is OK, refactoring the test code is a pretty save operation to do. [H]