Dr. Pizza MD
Dr. Pizza MD

Reputation: 11

Writing a unit test for a trigger

I need to write a unit test for this piece of code. I am just learning how to write unit tests. My understanding is that I need to start from the outside and work my way in for the if statements. What exactly am I trying to do in the unit test? How would you approach this task? Sorry I am a complete beginner. Thank you.

Trigger BuildComponentBI on Build_Component__c(before insert , before update) {
    if (Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)) {
        //Double CompVer = 0;
        double q = 0;
        for (Build_Component__c C: Trigger.new) {
            if (C.Manual_Override__c == False){

                List<Effort_Matrix__c> em = Effort_Matrix__c.getall().values();

                q = c.Scale__c;
                For(Effort_Matrix__c e:em){
                    if(e.Component_Name__c == c.Type__c){
                        if(e.Phase__c == 'Build'){
                            c.Estimated_Build_Hours__c = e.OOM__c * q;
                        }
                        if(e.phase__c == 'Analysis'){
                                c.Estimated_Analysis_Hours__c = e.OOM__c * q;
                            }
                        if(e.phase__c == 'SIT'){
                                c.Estimated_SIT_Hours__c = e.OOM__c * q;
                            }
                        if(e.phase__c == 'Deployment'){
                                c.Estimated_Deployment_Hours__c = e.OOM__c * q;
                            }
                        if(e.phase__c == 'UAT'){
                                c.Estimated_UAT_Hours__c = e.OOM__c * q;
                            }
                        if(e.phase__c == 'Unit Test'){
                                c.Estimated_Unit_Test_Hours__c = e.OOM__c * q;
                            }
                        if(e.phase__c == 'Design'){
                                c.Estimated_Design_Hours__c = e.OOM__c * q;
                            }
                        else{}
                }
             }      
            }

        }

    }
}

Upvotes: 1

Views: 522

Answers (1)

geekymartian
geekymartian

Reputation: 674

You don't test triggers directly. What you need to do is to create a unit test that inserts and updates the trigger object (Build_Component__c).

Example:

@isTest
private class BuildComponentTest {
    @isTest static void testManualOverrideFalseInsert() {
         Build_Component__c bc = new Build_Component__c();
         bc.Manual_Override__c = false;
         insert bc;
    }
}

Here you'll be testing the insert scenario when the Manual_Override__c boolean is false. Keep adding tests to cover the other conditions inside the if statement until you get the 100% of coverage.

Try to ask questions on the dedicated Salesforce SE you'll get answers quicker.

Upvotes: 1

Related Questions