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
TCAdminTCAdmin 

Theory Assistance

Hello All,

I am having a difficult time understanding some of the testing features and how you would do them in theory. If I create a trigger/class that prevents a record from being save you can do an assert on the failed update/insert. What can you test on if you are doing something other than that? I am not understanding it in my head. If I have a trigger/class that sends out an email when a field gets modified, what would you test on? If you are doing an insert but can't figure out how it could possibly error out, what would the next step be to get your testing requirements?
jgrenfelljgrenfell
Chris,

For inserts/updates, there are a few ways to use the System.assert methods, this is a clip from a test I wrote.  The trigger is designed to take the phone number from a Contact's parent Account and put it into the Contact's Phone field if the user left it null.  The test verifies that that actually happens:
Code:
  //Test account insert code  
  Account a = new Account(
      Name = 'Test Employer',
      BillingStreet = '32 Broadway',
      BillingCity = 'NY',
      BillingState = 'NY',
      BillingPostalCode = '10004',
      Borough__c = 'Manhattan',
      Phone = '(212) 422-4430',
      Type = 'Employer');
  insert a;
  
  Contact ec = new Contact(
      FirstName = 'Jessie',
      LastName = 'Grenfell',
      AccountId = a.Id,
      Title = 'Code Monkey',
      Preferred_Method_of_Contact__c = 'Phone',
      Best_Day_to_Call__c = 'Monday',
      Contact_Type__c = 'FUU Contact',
      MailingStreet = '32 Broadway',
      MailingCity = 'NY',
      MailingState = 'NY',
      MailingPostalCode = '10004');
  insert ec;
  
  ec = [Select Phone from Contact where Id = :ec.Id];
  System.assertEquals('(212) 422-4430', ec.Phone);

 
I haven't played with it yet, but for emails, I believe you could use the governor limits method "getEmailInvocations" to verify that your code is generating email(s).
jrotensteinjrotenstein
If I have a trigger/class that sends out an email when a field gets modified, what would you test on?
Where your trigger is not doing any data-related activities, you can't test it. (In fact, this type of notification could be done via Workflow Rules.)
If you are doing an insert but can't figure out how it could possibly error out, what would the next step be to get your testing requirements?
Make a test to show that the data WAS inserted. Maybe test for conflicting keys on Unique Contraints. Perhaps test for what should happen if the data being inserted already exists.
TCAdminTCAdmin

Thanks John,

I thought that I had missed something. The workflow rules would not work in this situation since the person getting notified is a couple objects away. Not the normal owner notification or field on the same object. Since these can't be tested, how would you get the test coverage required?

jrotensteinjrotenstein
Just write a test that causes your trigger to activate. It doesn't really have to test anything, just try to get 75% of your Trigger code executed.