You need to sign in to do that
Don't have an account?
Need help with writing test methods for 2 triggers
Could someone help me figure out how to get 75% coverage with this trigger I created on a custome object? Thank you.
// TRIGGER CODE=========================================
trigger addArtifactTemplates on NI_Change_Control__c (after insert)
{
if (trigger.isInsert)
{
for (NI_Change_Control__c nicc : Trigger.new)
{
// IF RECORD TYPE IS "Hosted_RFC"
if (nicc.RecordTypeId == '012P00000000G42')
{
NICC_Artifact__c a1 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Communication Plan');
insert a1;
NICC_Artifact__c a2 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Deployment Plan');
insert a2;
NICC_Artifact__c a3 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Monitoring Plan');
insert a3;
NICC_Artifact__c a4 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Rollback Plan');
insert a4;
NICC_Artifact__c a5 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Test Plan');
insert a5;
}
}
}
}
// MY TEST CLASS ATTEMPT ==============================
@isTest
private class TestNIChangeControl
{
static testMethod void testaddArtifactTemplates()
{
NI_Change_Control__c nicc = new NI_Change_Control__c (Request_Summary__c = 'Test Request Summary Text...', Date_Required__c = System.today());
insert nicc;
NICC_Artifact__c a1 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Communication Plan');
insert a1;
NICC_Artifact__c a2 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Deployment Plan');
insert a2;
NICC_Artifact__c a3 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Monitoring Plan');
insert a3;
NICC_Artifact__c a4 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Rollback Plan');
insert a4;
NICC_Artifact__c a5 = new NICC_Artifact__c(NI_Change_Control__c = nicc.Id, Type__c = 'Test Plan');
insert a5;
}
}
You need to specify the record type of the NI_Change_Control__c you create in the test so that it will fire the trigger.
Also - I'm not sure why you are inserting NICC_Artifact__c records in your test. Instead, you need to retrieve the NICC_Artifact__c records that were created by the trigger and do system.AssertEquals to verify they were created correctly.
All Answers
You need to specify the record type of the NI_Change_Control__c you create in the test so that it will fire the trigger.
Also - I'm not sure why you are inserting NICC_Artifact__c records in your test. Instead, you need to retrieve the NICC_Artifact__c records that were created by the trigger and do system.AssertEquals to verify they were created correctly.
Thank you dmcheng. Adding the RecordTypeId to the NI_Change_Control__c insert did the trick. Thank you very much. If I wasn't such a newbie to creating Test Classes I probably would have used the system.AssertEquals as you mentioned.