+ Start a Discussion
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Test code 90% need 100% on simple trigger system assert?

Hey there, 

I have a very simple trigger which is designed to create a new record in the Account status object and set the name to the account name + account status. I have gotten 90% coverage, but I am not sure how to phrase the system asserts.

I tried something along the lines of: System.assertEquals('Test Account Status',Account_status__c.name);

but it wouldnt work. Below is my code, thank you ina dvance for your time.


trigger CreateAccountStatus on Account (after insert) {

List <Account_status__c> AccStatus = new List <Account_status__c> ();
    // New Account status record
   
    for (Account a : Trigger.new) {
   
   if (a.Initial_Rating_Number__c > 10) {

        Account_status__c Ast = new Account_status__c (); //instantiate the object to put values for future record
       
        // now map opportunity fields to new vehicle object that is being created with this opportunity
       
        Ast.Account__c = a.id; // and so on so forth untill you map all the fields.
        //you can also assign values
        Ast.name = a.name+' Account Status';
       
        //once done, you need to add this new object to the list that would be later inserted.
        //don't worry about the details for now
       
        AccStatus.add(Ast);
       
       
       }
       
    }//end for o
   
    //once loop is done, you need to insert new records in SF
    // dml operations might cause an error, so you need to catch it with try/catch block.
    try {
        insert AccStatus;  
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
   
}


and 

///////////////////////test\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

@istest
private class TestCreateAccountStatus {
static testMethod void CreateAccountStatus()
{
account a1=new account(name='test',Client_Branch__c='Perth',Initial_Rating_Number__c=11);
insert a1;

}
}
zzj8810zzj8810
in the test
after insert a1, you should query the Account_status__c SObject you just inserted.
and  use the SObject you query to do assertion, like System.assertEquals('Test Account Status', Ast.name);
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
I have jumped it up to 92%, but cannot get that last 8%. Below si my revised code, thank you so much for your assistance.

@istest
private class TestCreateAccountStatus {
static testMethod void CreateAccountStatus()
{
account a1=new account(name='Test',Client_Branch__c='Perth',Initial_Rating_Number__c=11);
insert a1;
Account_status__c Ast=new Account_status__c(name='Test Account Status');

  Ast = [Select ID, Name, AccountID__c, Enquiry_Allocated_to_Office__c, Enquiry_submitted_date__c From Account_status__c WHERE AccountID__c =: a1.Id];

System.assertEquals('Test Account Status', Ast.name);
System.assertEquals(date.today(), Ast.Enquiry_submitted_date__c);
System.assertEquals(date.today(), Ast.Enquiry_Allocated_to_Office__c);

}
}



///////////////////////Trigger\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

trigger CreateAccountStatus on Account (after insert) {

List <Account_status__c> AccStatus = new List <Account_status__c> ();
    // New Account status record
   
    for (Account a : Trigger.new) {
   
   if (a.Initial_Rating_Number__c > 10) {

        Account_status__c Ast = new Account_status__c (); //instantiate the object to put values for future record
       
        // now map opportunity fields to new vehicle object that is being created with this opportunity
       
        Ast.Account__c = a.id; // and so on so forth untill you map all the fields.
        //you can also assign values
        Ast.name = a.name+' Account Status';
        Ast.AccountID__c = a.id;
        Ast.Enquiry_submitted_date__c = date.today();
        Ast.Enquiry_Allocated_to_Office__c = date.today();
       
        //once done, you need to add this new object to the list that would be later inserted.
        //don't worry about the details for now
       
        AccStatus.add(Ast);
       
       
       }
       
    }//end for o
   
    //once loop is done, you need to insert new records in SF
    // dml operations might cause an error, so you need to catch it with try/catch block.
    try {
        insert AccStatus;  
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
   
}