You need to sign in to do that
Don't have an account?
test class for trigger
Hi ,
I have written test class for the below trigger but covers only 45%, need help to fix it,
//trigger
trigger updateContactWithLatestCallStatus on Call_Report__c (after insert,after update){
if(Trigger.isInsert){
Call_Report__c cr=Trigger.new[0];
if(cr.contact__c!=null){
Contact cnt=new Contact(id=cr.contact__c,Call_Status__c=cr.status__c,Last_Call_Report_Date__c=cr.LastModifiedDate);
update cnt;
}
}else if(Trigger.isUpdate){
Call_Report__c cr=Trigger.new[0];
List<Call_Report__c> cntCrs=[select Id,name,contact__c,Status__c,Date__c from Call_Report__c where Contact__c=:cr.Contact__c and Date__c>= :cr.Date__c order by Date__c desc];
System.debug('Date__c ==> '+cntCrs.get(0).Date__c);
if(cntCrs!=null && cntCrs.size()>0 && cntCrs.get(0)!=null && cr.contact__c!=null){
Contact cnt=new Contact(id=cr.contact__c,Call_Status__c=cntCrs.get(0).status__c);
update cnt;
}
}
}
//test class
@isTest
private class updateContactWithLatestCallStatusTest
{
static testMethod void myUnitTest()
{
Call_Report__c cr = new Call_Report__c ();
//cr.Id='a0XW0000000LjTa';
cr.name='test';
cr.contact__c='003W0000009LKIz';
cr.Status__c='Scheduled';
cr.Date__c=date.today();
insert cr;
Contact con=new Contact();
//cnt.id='003W0000009LKIz';
con.Call_Status__c='Scheduled';
con.Last_Call_Report_Date__c=date.today();
con.LastName = 'Test';
con.Phone='6853235351';
con.Job_Title__c='Dentist';
con.Has_a_computer__c='yes';
insert con;
Thanks , Please help me
Although your test code should also be bulkified etc. (best practices) I'd add something like this:
@isTest
private class updateContactWithLatestCallStatusTest
{
static testMethod void myUnitTest()
{
Call_Report__c cr = new Call_Report__c ();
//cr.Id='a0XW0000000LjTa';
cr.name='test';
cr.contact__c='003W0000009LKIz';
cr.Status__c='Scheduled';
cr.Date__c=date.today();
insert cr;
//NEW CODE
Call_Report__c updateCR = [select Id, Name from Call_Report__c where Id = :cr.Id];
updateCR.Name = 'New Name';
update updateCR;
//END OF NEW CODE
Contact con=new Contact();
//cnt.id='003W0000009LKIz';
con.Call_Status__c='Scheduled';
con.Last_Call_Report_Date__c=date.today();
con.LastName = 'Test';
con.Phone='6853235351';
con.Job_Title__c='Dentist';
con.Has_a_computer__c='yes';
insert con;
All Answers
Your test class is only testing the INSERT of the Call_Report__c object, not an Update.
Add some more code in the test class to retrieve the inserted Call_Report__c, make a change to it, then use Database.Update
k then i should create a callreport instance again and pass all the fields as done for inserting and last database.upsert to be used, am i right
thanks
Although your test code should also be bulkified etc. (best practices) I'd add something like this:
@isTest
private class updateContactWithLatestCallStatusTest
{
static testMethod void myUnitTest()
{
Call_Report__c cr = new Call_Report__c ();
//cr.Id='a0XW0000000LjTa';
cr.name='test';
cr.contact__c='003W0000009LKIz';
cr.Status__c='Scheduled';
cr.Date__c=date.today();
insert cr;
//NEW CODE
Call_Report__c updateCR = [select Id, Name from Call_Report__c where Id = :cr.Id];
updateCR.Name = 'New Name';
update updateCR;
//END OF NEW CODE
Contact con=new Contact();
//cnt.id='003W0000009LKIz';
con.Call_Status__c='Scheduled';
con.Last_Call_Report_Date__c=date.today();
con.LastName = 'Test';
con.Phone='6853235351';
con.Job_Title__c='Dentist';
con.Has_a_computer__c='yes';
insert con;
Thanks a lot my friend, it worked, i have another doubt,
I am fetching the the time field in my controller, its displyaing it in indian standard time, say if its 14.00 its displayng it as 2.00, i want to display it as 14.00 the user locale time, i used output text in vf page used with sharing in controller, but didnt work
can u help me on this also plzzz