+ Start a Discussion
Savi3Savi3 

Test class error-Assertion Failed: Expected: 20, Actual: 0

Hi ,

 

     I have a trigger which generates the standard field, Name of a custom object with  'a string + name of the corresponding Contact' before inserion/Updation.

My trigger:--

____________________________________________________________________________________________

trigger trgname on cusobj__c (before insert,before update) {
Set<ID> conid = new Set<ID>();
List<contact> lstContact = new List<contact>();
Map<ID,contact> Mapname = new Map<ID,contact>();
for( cusobj__c tar :Trigger.New)
{
conid.add(tar.Contact__c); // Storing all the contact ids for which the name needs to be used
}
system.debug('Inside the trigger to update the name');
//Getting all the contact records in a list to be used in the name field
lstContact = [select id,name,firstname,lastname from contact where id in : conid];

for(integer i =0; i <lstcontact.size(); i++)
{
Mapname.put(lstcontact[i].id,lstcontact[i]); // Putting the contact id and name in a map
}

for( cusobj__c tar :Trigger.New)
{
// tar.name = 'TD Rem - ' + mapname.get(tar.Contact__c).firstname +' ' + mapname.get(tar.Contact__c).lastname;
tar.name = 'TD Rem - ' + mapname.get(tar.Contact__c).name;
}

}

_____________________________________________________________________________________________

 

I do have a test class for this trigger . But getting  error 'Assertion Failed: Expected: 20, Actual: 0' 

 

My Testclass:-

____________________________________________________________________________________________

@isTest
Private class testTrgTrgname{
Static testMethod void methodname(){
Contact con = new Contact();
con.FirstName= 'fname';
con.LastName = 'lname';

insert con;
System.debug('value for contact name:' + con);
Contact con1 = new Contact();
con1.LastName = 'lname1';
insert con1;
//Insert test data

cusobj__c Tmapt = new cusobj__c();
Tmapt.Appointment_Created__c = True;
Tmapt.Target_date__c = System.today()+3;
Tmapt.Contact__c = con.id;

insert Tmapt;
System.debug('value for Tmapt name:' + Tmapt.Name);
System.debug('value for Tmapt :' + Tmapt);
List<cusobj__c> ltmd = new List<cusobj__c>();
ltmd =[Select Contact__c from cusobj__c where id =:Tmapt.id];
System.assertEquals(1,ltmd.Size());

System.assertEquals('TD Rem-fname lname', Tmapt.Name);
cusobj__c Tpt = new cusobj__c();
Tpt.Appointment_Created__c = True;
Tpt.Target_date__c = System.today()+ 5;
Tpt.Contact__c =con1.id;
insert Tpt;
System.assertEquals(Tpt.Name, con1.name);
Contact newcon = new Contact();
newcon.lastname = 'lnewcon';
insert newcon;
//cusobj__c Ltar =[Select Contact__c from cusobj__c where id := Tmapt.id];
//Tmapt.Cotnact__c = newcon.id;
//update Tmapt;

}
}

______________________________________________________________________________________________

  

 

Any idea?? Please help...

 

 

Thank You,

sav3

Best Answer chosen by Admin (Salesforce Developers) 
sbbsbb

After Tmapt has been inserted, you need to query it again (along with the name) before you want to use AssertEquals. Otherwise, it doesn't know what the name is (which is being computed in your trigger).

 

Also, note that in your assertion, you need to add a space on either side of "-" (because that it is what your trigger is doing).

 

Hope that helps.

All Answers

sbbsbb

Which assertion is failing? I don't see any assertion that is expecting 20 in your test code.

Savi3Savi3

Hi,

In debug log its saying like this...

 

10:16:27.447 (4447242000)|USER_DEBUG|[8]|DEBUG|value for contact name:null

 

EXCEPTION_THROWN|[24]|System.AssertException: Assertion Failed: Expected: TD Rem-fname lname, Actual: null
10:16:27.518 (4518982000)|FATAL_ERROR|System.AssertException: Assertion Failed: Expected: TD Rem-fname lname, Actual: null

 

Thank You,

Sav3

sbbsbb

After Tmapt has been inserted, you need to query it again (along with the name) before you want to use AssertEquals. Otherwise, it doesn't know what the name is (which is being computed in your trigger).

 

Also, note that in your assertion, you need to add a space on either side of "-" (because that it is what your trigger is doing).

 

Hope that helps.

This was selected as the best answer