0

I have a test class that is only covering my trigger 48% and I want to get it as close to 100% as possible. It appears there is one section that I am not covering when using the Developer Console but I am not sure how to modify the class to include these rows. The rows where there is no coverage are the rows between:

for(Opportunitylineitem Opplist :opplinitemlist ){ and RecipeList.add(rec);

Trigger:

trigger InsertRecipeSheets on Opportunity (after update){

    List<Recipe_Sheet__c> RecipeList =new List<Recipe_Sheet__c>();
    List<Opportunitylineitem> opplinitemlist =new List<Opportunitylineitem>();
    List<Opportunity> Opportunitylist =new List<Opportunity>();

    for(Opportunity opp :trigger.new){
        if(opp.Create_Recipe_Sheet_s__c==True){       
        Opplinitemlist =[select Id,PricebookEntry.Product2.Name,PricebookEntry.Product2.Do_Not_Discount__c,Wall_Mount_Stem__c,Quantity,PricebookEntry.Product2.Record_Type_ID__c,Total_Drop__c,Finish_s__c,Notes__c, For_Vaulted_Ceiling__c,PricebookEntry.Product2.id from Opportunitylineitem where Opportunityid =: opp.id AND Product2.Do_Not_Discount__c=False AND Product2.Record_Type_ID__c<>''];
        Opportunitylist  =[Select Accountid from Opportunity where Id =: opp.id];

        for(Opportunitylineitem Opplist :opplinitemlist ){
        Recipe_Sheet__c rec =new Recipe_Sheet__c();
        rec.Opportunity__c =Opportunitylist[0].Id;
        rec.product__c=opplist.PricebookEntry.Product2.Id;
        rec.Name=opplist.PricebookEntry.Product2.Name;
        rec.For_Vaulted_Ceiling__c=opplist.For_Vaulted_Ceiling__c;
        rec.Wall_Mount_Stem__c=opplist.Wall_Mount_Stem__c;
        rec.Finish_s__c=opplist.Finish_s__c;
        rec.Notes__c=opplist.Notes__c;
        rec.Total_Drop__c=opplist.Total_Drop__c;
        rec.Quantity__c=opplist.Quantity;
        rec.Status__c='Received by Production';
        rec.RecordTypeID=opplist.PricebookEntry.Product2.Record_Type_ID__c;
        RecipeList.add(rec);
        }
        insert RecipeList;
        } 
        }  
    for(Opportunity Opps :Opportunitylist ){
    Opps.Create_Recipe_Sheet_s__c=False;
    }
    update Opportunitylist;


}

Test Class:

    @IsTest (OnInstall=true SeeAllData=true) private class TEST_InsertRecipeSheets{


private static testMethod void myUnitTest() {

Account acc=new Account(
    Name='test',
    Resale_Certificate_Number_Tax_ID__c='NA',
    BillingStreet='123',
    BillingCity='portsmouth',
    BillingState='NH',
    BillingpostalCode='03801',
    BillingCountry='US'
);
insert acc;   


Pricebook2 pb = [select id from Pricebook2 where IsStandard = true limit 1];   

//Product2 prods=[select id,name,Do_Not_Discount__c,Record_Type_ID__c  from Product2 where IsActive = true AND Record_Type_ID__c<>'' AND Do_Not_Discount__c= FALSE];   

    // create the product
    Product2 p1= new Product2(
        name='CLOUD 19',
        IsActive=true,
        Description='My Product',
        ProductCode='12345',
        Do_Not_Discount__c=False
        );
    insert p1;

    // create 2nd product
    Product2 p2= new Product2(
        name='TWIG 5',
        IsActive=true,
        Description='My Base Product',
        ProductCode='11111',
        Do_Not_Discount__c=False
        );
    insert p2;


    // create the pricebookentry
        PricebookEntry pbe1 = new PricebookEntry(
        Pricebook2Id=pb.id,
        Product2Id=p1.id,
        UnitPrice=1.00,
        IsActive=true,
        UseStandardPrice=false
    );
    insert pbe1; 

     // create 2nd pricebookentry
        PricebookEntry pbe2 = new PricebookEntry(
        Pricebook2Id=pb.id,
        Product2Id=p2.id,
        UnitPrice=1.00,
        IsActive=true,
        UseStandardPrice=false
    );
    insert pbe2; 


    Item__c i1 =new Item__c(
    Name='Test'
    );
    insert i1;  

    Item__c i2 =new Item__c(
    Name='Test'
    );
    insert i2; 

    Product_Item__c pi1=new Product_Item__c(
    Product__c=p1.id,
    Item__c=i1.id,
    Quantity__c=1,
    External_Wall_Mount_Vaulted_Ceiling__c=2,
    Standard_Item__c=True
    );
    insert pi1;

    Product_Item__c pi2=new Product_Item__c(
    Product__c=p2.id,
    Item__c=i2.id,
    Quantity__c=2,
    External_Wall_Mount_Vaulted_Ceiling__c=3,
    Standard_Item__c=True
    );
    insert pi2;

    Product_Item__c pi3=new Product_Item__c(
    Product__c=p2.id,
    Item__c=i2.id,
    Quantity__c=null,
    External_Wall_Mount_Vaulted_Ceiling__c=null,
    Standard_Item__c=False
    );
    insert pi3;


    // create the opportunity
        Opportunity opp1 = new Opportunity(
        name='Test Opp 1',
        StageName = 'Balance Received',
        CloseDate = Date.newInstance(2009,01,01), 
        Production_Date__c=Date.newInstance(2009,01,01),
        Production_Status__c='Received By Production',
        Create_Recipe_Sheet_s__c=True,
        Pricebook2Id=pb.id         
    );
    insert opp1;


    // add the line item
    OpportunityLineItem oli1 = new OpportunityLineItem(
    Quantity = 1,
    PricebookEntryId = pbe1.id,
    OpportunityId = opp1.id,
    For_Vaulted_Ceiling__c='No',
    Wall_Mount_Stem__c='No',
    UnitPrice=1
    );
    insert oli1;

    OpportunityLineItem oli2 = new OpportunityLineItem(
    Quantity = 2,
    PricebookEntryId = pbe2.id,
    OpportunityId = opp1.id,
    For_Vaulted_Ceiling__c='No',
    Wall_Mount_Stem__c='No',
    UnitPrice = 2
    );  
    insert oli2; 

    update opp1;

    Recipe_Sheet__c RS1 =new Recipe_Sheet__c(
    Opportunity__c=opp1.id,
    Product__c=oli1.PricebookEntry.Product2.Id,
    Status__c='Inventory Committed',
    For_Vaulted_Ceiling__c=oli1.For_Vaulted_Ceiling__c,
    Wall_Mount_Stem__c=oli1.Wall_Mount_Stem__c,
    Name='Test'
    );
    insert RS1; 


    RS1 =  [SELECT Id,Product__c,Status__c FROM Recipe_Sheet__c 
             WHERE Id = :RS1.Id];

    System.assertEquals('Inventory Committed', RS1.Status__c);

    Recipe_Sheet__c RS2 =new Recipe_Sheet__c(
    Opportunity__c=opp1.id,
    Product__c=oli2.PricebookEntry.Product2.Id,
    Status__c='Inventory Committed',
    For_Vaulted_Ceiling__c=oli2.For_Vaulted_Ceiling__c,
    Wall_Mount_Stem__c=oli2.Wall_Mount_Stem__c,
    Recalculate_Recipe_Sheet_Items__c = True,
    Name='Test'
    );
    insert RS2; 


    RS2 =  [SELECT Id,Product__c,Status__c FROM Recipe_Sheet__c 
             WHERE Id = :RS2.Id];

    System.assertEquals('Inventory Committed', RS2.Status__c);

    Recipe_Sheet_Item__c ri1=new Recipe_Sheet_Item__c(
    Recipe_Sheet__c=RS1.id,
    Item__c=i1.id,
    Quantity__c=1
    );
    insert ri1;

    ri1 =  [SELECT Id,Recipe_Sheet__c,Item__c, Quantity__c FROM Recipe_Sheet_Item__c 
             WHERE Id = :ri1.Id];

    System.assertEquals(1, ri1.Quantity__c);


    Recipe_Sheet_Item__c ri2=new Recipe_Sheet_Item__c(
    Recipe_Sheet__c=RS1.id,
    Item__c=i2.id,
    Quantity__c=3
    );
    insert ri2;




          }
}
0

You never update your Opportunity after you create line items for it. Try putting:

update opp1;

at the end.

EDIT:

Also try removing AND Product2.Do_Not_Discount__c=False AND Product2.Record_Type_ID__c<>'' one at a time and see if that makes a difference. You might need to add an additional filter to the product you return in your test class.

  • I added the "update opp1;" after "insert oli2;" and there was no change in coverage. – Grace Jul 31 '14 at 19:44
  • Added an edit to my answer for you to try out. – dphil Jul 31 '14 at 19:49
  • Just tried that. No change unfortunately. – Grace Aug 1 '14 at 16:26
  • I edited the Test Class above to the most recent version I have. – Grace Aug 1 '14 at 17:15
0

In your test class, you missed to set the field `Product2.Do_Not_Discount__c=False' and adding a recordtype to it, which fires your trigger.

Also like dphil said, update your opportunity after inserting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.