1

I need help writing the test class, I am getting only 70%, I don't know what else to do. I will appreciate any help!

Trigger Code:

  trigger updateOSOTrigger on SO_Detail__c (before insert, before update, before delete) {


   List <Outbound_Sales_Order__c> oso = new List<Outbound_Sales_Order__c>();  

   if(Trigger.isInsert || Trigger.isUpdate){
    for(SO_Detail__c p : Trigger.new){
        if(!String.isEmpty(p.Outbound_Sales_Order__c)){
            oso.add(new Outbound_Sales_Order__c(

              Id = p.Outbound_Sales_Order__c,
              x1_Issue__c = String.valueof(p.x1_Issue__c), 
              x2_Issue__c = String.valueof(p.x2_Issue__c), 
              x3_Issue__c = String.valueof(p.x3_Issue__c), 
              x4_Issue__c = String.valueof(p.x4_Issue__c), 
              x5_Issue__c = String.valueof(p.x5_Issue__c), 
              x6_Issue__c = String.valueof(p.x6_Issue__c)  

            ));
      }
    }
  }

  if(Trigger.isDelete){
    for(SO_Detail__c p : Trigger.old){
        if(!String.isEmpty(p.Outbound_Sales_Order__c)){
            oso.add(new Outbound_Sales_Order__c(

              Id = p.Outbound_Sales_Order__c,
              x1_Issue__c = '', 
              x2_Issue__c = '', 
              x3_Issue__c = '', 
              x4_Issue__c = '', 
              x5_Issue__c = '', 
              x6_Issue__c = ''  

            ));
      }
    }
  }

 if(!oso.isEmpty()) update new List<Outbound_Sales_Order__c>(oso);

}

Test Class Code

  @isTest
    public class test_updateOSOTrigger {
    static testMethod void updateOSOTrigger_Test() {
    Profile pf = [Select Id from Profile where Name = 'System Administrator'];
    User u55 = new User();
    u55.FirstName = 'Test';
    u55.LastName = 'User';
    u55.Email = '[email protected]';
    u55.CompanyName = 'test.com';
    u55.Title = 'Test User';
    u55.Username = '[email protected]';
    u55.Alias = 'testuser';
    u55.CommunityNickname = 'Test User';
    u55.TimeZoneSidKey = 'America/Mexico_City';
    u55.LocaleSidKey = 'en_US';
    u55.EmailEncodingKey = 'ISO-8859-1';
    u55.ProfileId = pf.Id;
    u55.LanguageLocaleKey = 'en_US';
   insert u55;

    system.runAs(u55){

   Account account = new Account(Name = 'Test Account1');
    insert account;

   NRProducts__c product2 = new NRProducts__c(Account__c = account.Id, Name = '105823437');
   insert product2;


   Entity__c ent = new Entity__c(name='CarlosStore') ;
   insert ent;  


   Outbound_Sales_Order__c oso = new Outbound_Sales_Order__c(Entity__c= ent.id);
   oso.X1_Issue__c = '2';
   oso.X2_Issue__c = '2';
   oso.X3_Issue__c = '2';
   oso.X4_Issue__c = '2';
   oso.X5_Issue__c = '2';
   oso.X6_Issue__c = '2';
   insert oso;
   update oso;    

   SO_Detail__c detail = new SO_Detail__c(Outbound_Sales_Order__c = oso.id, NRProducts__c = product2.id );
   detail.X1_Issue__c = 2;
   detail.X2_Issue__c = 2;
   detail.X3_Issue__c = 2;
   detail.X4_Issue__c = 2;
   detail.X5_Issue__c = 2;
   detail.X6_Issue__c = 2;
   insert detail;
   update detail;


  Test.startTest();

  System.assertequals( String.valueof(detail.X1_Issue__c) , oso.X1_Issue__c);
  System.assertequals( String.valueof(detail.X2_Issue__c) , oso.X2_Issue__c);
  System.assertequals( String.valueof(detail.X3_Issue__c) , oso.X3_Issue__c);
  System.assertequals( String.valueof(detail.X4_Issue__c) , oso.X4_Issue__c);
  System.assertequals( String.valueof(detail.X5_Issue__c) , oso.X5_Issue__c);
  System.assertequals( String.valueof(detail.X6_Issue__c) , oso.X6_Issue__c);

  Test.stopTest();

    }
    }
    }

1 Answer 1

2

It looks like you are not testing the Delete case in the trigger. Trying deleting one of the records you created in the test.

BTW, if you are doing your testing in the Developer Console, you can click on the line where the class name and code % covered are show. It will take you to a window where the covered code is highlighted and non-covered code isn't. Doing that always helps me see what I need to add to the test.

The code for delete would be something like

SO_Detail__c detail = new SO_Detail__c(Outbound_Sales_Order__c = oso.id, NRProducts__c = product2.id );
detail.X1_Issue__c = 2;
detail.X2_Issue__c = 2;
detail.X3_Issue__c = 2;
detail.X4_Issue__c = 2;
detail.X5_Issue__c = 2;
detail.X6_Issue__c = 2;
insert detail;
update detail;

delete detail;  //This is to test the delete branch in the trigger

I'd move the startTest higher up in the code. Any code between startTest and stopTest gets a new set of governor limits. If the test+trigger code get close to any limits, this will prevent you from bumping into them. Having it where it is won't hurt you, but these statements are really meant to wrap the code that is actually doing the test. More on starTest here.

Note: can't add an image of this just now, but I'll try to do so later today.

4
  • You are right, the delete case is not being tested, how can I do it?
    – Carlos
    Jul 16, 2014 at 18:31
  • I recommend moving "Test.startTest();" higher in your test class.
    – Jagular
    Jul 16, 2014 at 18:44
  • could you please tell where exactly? and could you please explain me why. thank you in advance for your time
    – Carlos
    Jul 16, 2014 at 18:47
  • Edited my answer to mention startTest.
    – Jagular
    Jul 16, 2014 at 19:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .