Can anybody help me please to finish this test class, I am new in testing class and I spent the whole day trying to get coverage for this trigger, so far I get 33% and looks like the only part of the code that is getting coverage is the AggregateResult.
Here is the trigger:
Trigger OSOCount on Purchase_Order__c (before update) {
for(AggregateResult result : [
SELECT PO_Lookback__c, SUM(Total_Qty_to_Ship__c)
FROM Outbound_Sales_Order__c
WHERE PO_Lookback__c IN :Trigger.new
GROUP BY PO_Lookback__c
]){
Purchase_Order__c prod = Trigger.newMap.get((string)result.get('PO_Lookback__c'));
prod.OSO_Count__c= (double)result.get('expr0');
}
}
here is the testing class I am working on, I will appreciate any help
@istest
public class Test_OSOCount
{
private static testmethod void TestOSOCount ()
{
Account account1 = new Account(Name = 'Test Account1');
insert account1;
Purchase_Order__c aa = new Purchase_Order__c (Account__c = account1.id);
insert aa;
aa.OSO_Count__c=5;
update aa;
test.StartTest();
update aa;
test.StopTest();
System.assertEquals(5,aa.OSO_Count__c);
}
}
Attribution to: Carlos
Possible Suggestion/Solution #1
One problem is that while you do insert a Purchase_Order__c
you never update it and the trigger is a "before update" trigger.
PS OK you just changed the test code. My answer relates to version 2 of the question.
PPS The test needs to wire up the objects and then update the Purchase_Order__c
(assuming the trigger does what is required):
@istest
public class Test_OSOCount {
private Static testmethod void TestOSOCount () {
Account a = new Account(Name = 'Test Account1');
insert a;
Purchase_Order__c po = new Purchase_Order__c(Account__c = a.id);
insert po
insert new Outbound_Sales_Order__c[] {
new Outbound_Sales_Order__c(PO_Lookback__c = po.Id, Total_Qty_to_Ship__c = 4),
new Outbound_Sales_Order__c(PO_Lookback__c = po.Id, Total_Qty_to_Ship__c = 9)
};
update po;
System.assertEquals(4 + 9, [
select OSO_Count__c
from Purchase_Order__c
where Id = :po.Id
].OSO_Count__c);
}
}
Attribution to: Keith C
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33633