You need to sign in to do that
Don't have an account?

Help with writing a Test Class? + a question
Hello,
I need help with getting 100% coverage for my trigger below. Current code coverage is 37% (3/8)
trigger RunRules on Lead (After update) { string fname = UserInfo.getFirstName(); for (Lead lead : Trigger.new) { if (fname == 'Fred' && (lead.L_Rate__c == 'A' ||lead.L_Rate__c == 'B') && LeadAssignRuleUtil.firstTime && Lead.ISConverted == False && Lead.OwnerID != '00G70000001ifuqFWE' ) { Database.DMLOptions dmo = new Database.DMLOptions(); dmo.assignmentRuleHeader.useDefaultRule= true; lead.setOptions(dmo); LeadAssignRuleUtil.firstTime = false; Database.Update(lead); } } }
My Question:
How do I know what Test Class is doing the code coverage for my Trigger? When I look under Setup -> Develop -> Apex Classes, I don't see any classes with a name close to or similar to the associated trigger name "RunRules" but I know it is getting partially covered (Current code coverage is 37%) by an unknown test class.
Not sure if there is an easy way to identify the test(s) contributing to coverage, unless you run them all one by one. But do remember that any apex test which updates lead records would cover this trigger partially. By just looking at your trigger, it seems like you are missing coverage on the code within if loop. To get 100% code coverage, you can write an explicit test which updates leads setting the data in such a way that the if loop is entered. And of course, you want to assert on the changed behavior to make the test more meaningful, and name the test after the trigger (something like RunRulesTriggerTest) so it's easier for you in future to locate the test associated with the trigger. Hope this helps.
Thanks for answering my question. It makes sense. For the test class problem, you are correct in saying that everything in the if statement needs to get covered.
The problem is I don't know how to cover it. Here is my test class:
Please give me specific code to achieve 100% code coverage.
Thanks.
Try adding below at the top of your test method:
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;
lead.setOptions(dmo);
LeadAssignRuleUtil.firstTime = false;
Database.Update(lead);
Anything else?
@k_bentsen - I got this resolved based on your suggestion. Thanks for pointing me in the right direction.