I am new to Apex coding and I am currently working on an example where I have two custom objects:
Disease__c and Patient__c
These two custom object have a Master Detail Relationship (DiseaseR__c), where Disease__c is the Master Side.
I now want to write a test method to check the following scenario.
Disease__c has a status field "Status__c". I want to send an email once the status field has changed from "under review" to "valid". Below is my code for the test method.
public static testMethod void NotifyPatients_UnitTest()
{
User u1 = [SELECT Id FROM User WHERE profileId IN (SELECT id FROM Profile WHERE Name = 'System Administrator') LIMIT 1];
System.RunAs(u1)
{
ApexTrigger myTrigger = [Select Id, Status from ApexTrigger where name='diseaseTrigger'];
Boolean isActive = (myTrigger.Status == 'Active');
Account acc = new Account(Name='Test Account');
insert acc;
Contact con = new Contact( FirstName='Test',
LastName='User',
Phone='01234 567890',
Email='[email protected]',
AccountId=acc.id
);
insert con;
RecordType rt = [SELECT Id FROM RecordType WHERE Name='contageous' and SObjectType='Disease__c'];
Disease__c dis= new Disease__c( RecordType = rt,
Status__c='under review'
);
insert dis;
Patient__c pp = new Patient__c( Description__c = 'Test',
DiseaseR__c = dis.Id,
Contact__c = con.Id
);
insert pp;
/*this is where I am starting to fail - how do I create an update in my test?
my code is obviously wrong, but I cannot figure out the correct syntax*/
dis= ( RecordType = rt,
Status__c='valid'
);
update dis;
/*The other thing is now, how do I assert that an email is sent after the field was updated?*/
}
}
Thank you so much for your help in advance. Reena.