function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
daniel1110daniel1110 

trigger stuck at 62% coverage

Hi,

 

I have the following trigger, but it's my test code is not covered for line 10 and lines 15 and 16. I'm new to this and any suggestion would be appreciated.

 

 line	 source
 1	  trigger updateSCBonTask1 on Task (after update) {
 2	  
 3	      Set<Id> SCBIds = new Set<Id>();
 4	  
 5	      for(Task t: [SELECT Subject,WhatId, Status FROM Task WHERE ID IN:trigger.new]){
 6	  
 7	          String wId = t.WhatId;
 8	      
 9	          if(wId!=null && !SCBIds.contains(t.WhatId) && t.Status == 'Completed' && t.Subject.Contains('Collect Enrollment Information')){
 10	              SCBIds.add(t.WhatId);
 11	          }
 12	      }
 13	      
 14	      for(Stem_Cell_Bank__c a:[SELECT stage__c FROM Stem_Cell_Bank__c WHERE ID IN:SCBIds]){
 15	                   a.stage__c = 'Process Payment';
 16	          update a; 
 17	      }  
 18	  
 19	  }

 My test code:

 

@isTest
public class updateSCBonT1Test {
         static testMethod void testupdateSCBonT1(){
             Stem_Cell_Bank__c s = new Stem_Cell_Bank__c();
             s.name = 'Test';
             s.account__c = '001c000000AfLHOAA3';
             test.startTest(); 
             insert(s); 
             LIST<Stem_Cell_Bank__c> a = [SELECT Id, Stage__c FROM Stem_Cell_Bank__c WHERE Name = 'Test'];
             Task testTask = new Task(Subject = 'Test', whatId = a[0].ID); 
             insert(testTask); 
             Stem_Cell_Bank__c updatedStem_Cell_Bank = [SELECT stage__c FROM Stem_Cell_Bank__c WHERE name = 'Test'];
             system.AssertEquals('Collect Enrollment Information',updatedStem_Cell_Bank.stage__c);
             testTask.Status='Completed';
             update(testTask);
             Stem_Cell_Bank__c checkupdate = [SELECT stage__c FROM Stem_Cell_Bank__c WHERE name = 'Test'];
             system.AssertEquals('Process Payment',checkupdate.stage__c);
             test.stopTest();
        }
}

 thank u in advance~

 

Bhawani SharmaBhawani Sharma
testTask.Status='Completed';
testTask.WahtId = s.Id;
update(testTask);

Populate what id with s.Id before updating the task.

Anyway, I saw you are using hardcoded account Id in your test method. This is not a good practice. Create an Account record in test class itself and then use this new record.
daniel1110daniel1110

Thank you Tech Force for your suggestion.

 

I've taken your suggestion, which makes good logical sense, but the update is still failing for some reason.

 

The modified code is as follows:

 

@isTest
public class updateSCBonT1Test {
         static testMethod void testupdateSCBonT1(){
             Stem_Cell_Bank__c s = new Stem_Cell_Bank__c();
             s.name = 'Test';
             s.account__c = '001c000000AfLHOAA3';
             test.startTest(); 
             insert(s); 
             LIST<Stem_Cell_Bank__c> a = [SELECT Id, Stage__c FROM Stem_Cell_Bank__c WHERE Name = 'Test'];
             Task testTask = new Task(Subject = 'Test', whatId = a[0].ID); 
             insert(testTask); 
             Stem_Cell_Bank__c updatedStem_Cell_Bank = [SELECT stage__c FROM Stem_Cell_Bank__c WHERE name = 'Test'];
             system.AssertEquals('Collect Enrollment Information',updatedStem_Cell_Bank.stage__c);
             testTask.whatId = s.Id;
             testTask.Status='Completed';
             update(testTask);
             Stem_Cell_Bank__c checkupdate = [SELECT stage__c FROM Stem_Cell_Bank__c WHERE name = 'Test'];
             system.AssertEquals('Process Payment',checkupdate.stage__c);
             test.stopTest();
        }
}

 

The result I get back is:

 

Pass/Fail Fail Error Message System.AssertException: Assertion Failed: Expected: Process Payment, Actual: Collect Enrollment Information Stack Trace Class.updateSCBonT1Test.testupdateSCBonT1: line 18, column 1

 

Any thoughts?

 

Still trying to figure out how to create account record in the test class