You need to sign in to do that
Don't have an account?
GRStevenBrookes
Help with Test Class
Hi,
Dont quite get test classes yet.... could someone help me write one for the following:
trigger UpdateCountOfScriptBuildsInProgress on Case (before update) { // service agreements are on the case record Set<Id> saIds=new Set<Id>(); for (Case cs : trigger.new) { saIds.add(cs.Service_Agreement__c); } List<Service_Agreement__c> sas=[select id, COUNT_of_Script_Builds_In_Progress__c, (select id from Cases__r where Reason = 'New Build'and Status = 'In Progress') from Service_Agreement__c where id in :saIds]; for(Service_Agreement__c sa : sas) { sa.COUNT_of_Script_Builds_In_Progress__c = sa.Cases__r.size(); } update sas; }
thanks in advance.
Hi,
Assuming that your trigger works fine, All you need for trigger code coverage is the records that you are going to insert/update/delete.
So in your case , you are using records from 2 objects :
1. Case
2. Service_Agreement__c
So just create a test service agreement record and a test case record which satisfies your Query conditions.
This is what you need to do :
@isTest
private class testLeadTrigger
{
static testMethod void myUnitTest()
{
Service_Agreement__c sa = new Service_Agreement__c();
// put all your required fields for Service_Agrement__c , else the insert will fail, say for example Name is required field then
sa.Name = 'test';
insert sa;
Case c = new Case();
c.Status = 'In Progress'; // this satisfies one of your conditions
c.Origin = 'Phone'; // its a required field
c.Reason = 'New Build'; //this satisfies the other conditon
c.Service_Agreement__c = sa.Id;
insert c;
//Since it is a before update trigger, you need to update the case you just inserted.
c.Origin = 'Web';
update c; // this statement will fire the trigger and it should cover your code too.
}
}
Hope it helps :)
All Answers
Hi,
Create a class like below
@isTest
private class Test_UpdateCountOfScriptBuildsInProgress
{
static testMethod void myUnitTest()
{
//Create Case Record
Case objCase=new Case();
//just Pass the Required fied information
insert objCase;
//Then Create Service_Agreement__c
Service_Agreement__c objService=new Service_Agreement__c();
//just Pass the Required fied information
insert objService;
}
}
Let me know if yoy need any other help on this.
Thanks
Hi,
Assuming that your trigger works fine, All you need for trigger code coverage is the records that you are going to insert/update/delete.
So in your case , you are using records from 2 objects :
1. Case
2. Service_Agreement__c
So just create a test service agreement record and a test case record which satisfies your Query conditions.
This is what you need to do :
@isTest
private class testLeadTrigger
{
static testMethod void myUnitTest()
{
Service_Agreement__c sa = new Service_Agreement__c();
// put all your required fields for Service_Agrement__c , else the insert will fail, say for example Name is required field then
sa.Name = 'test';
insert sa;
Case c = new Case();
c.Status = 'In Progress'; // this satisfies one of your conditions
c.Origin = 'Phone'; // its a required field
c.Reason = 'New Build'; //this satisfies the other conditon
c.Service_Agreement__c = sa.Id;
insert c;
//Since it is a before update trigger, you need to update the case you just inserted.
c.Origin = 'Web';
update c; // this statement will fire the trigger and it should cover your code too.
}
}
Hope it helps :)
Hi thanks to both of you for your replies - however I am getting the following for both:
UpdateCountOfScriptBuildsInProgress Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
Hi, You might make some changes to your trigger to make it a bit more efficient...
I also recognize that you're not doing this as an Insert trigger. I'd think that you want to handle the insert case as well, however perhaps you don't allow records to be created in the state that you're trying to count. So, I'll skip that. Likewise the Delete case.
You're also missing the situation where a case is edited so that it points to a different Service Agreement than it used to.
So, the trigger:
And, for testing:
Hope it helps, best, Steve.
p.s. Note: I just typed this in, I'm sure there are some problems with it. :-) S.