function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Leonardo Girgenti 5Leonardo Girgenti 5 

help with testing class

hi there, 
could anyone help me with a test class for  this trigger?
thanks in advance

trigger MeetingParticipantTrigger on Meeting_Participants__c(before insert){
Set<Id> contactIds = new Set<Id>();
for(Meeting_Participants__c mp : Trigger.new){
contactIds.add(mp.Contact__c);
}
Map<Id,Contact> contacts = new Map<Id,Contact>([Select AccountId From Contact Where Id in :contactIds]);
for(Meeting_Participants__c mp : Trigger.new){
Contact c = contacts.get(mp.Contact__c);
mp.Account2__c = c.AccountId;
}
}

 
David ZhuDavid Zhu
for your reference. you may need to twick the code a little bit. espeically at creating  meeting_participants__c instance.

public static testmethod void test1()
{
    Account a = new Account(name ='test account');
    insert a;

    Contact c = new Contact(name='test contact',account=a);
    insert c;

    meeting_participants__c mp = new meeting_participants__c(name = 'testmp',contact=c);   
    insert mp;

    system.assertequals(mp.account2__c,c.accountid);
}
Leonardo Girgenti 5Leonardo Girgenti 5
not working....
i get an error when saving the class. 
David ZhuDavid Zhu
try this one, but you have to make the the highlighted line work yourself since it depends how your object fields and relationship defined.
public static testmethod void test1()
{
    Account a = new Account(name ='test account');
    insert a;

    Contact c = new Contact(AccountId=a.Id,lastname='testing',firstname='apex');
    insert c;

    meeting_participants__c mp = new meeting_participants__c(name = 'testmp',contact=c);   
    insert mp;

    system.assertequals(mp.account2__c,a.id);
}