We did a minor modification to our code in the If/Else statement in our class, now I am trying to reach 100% test coverage. As you can see in my test class below, i inserted a new contact as coTeacher and a new class as newCLass and populated the fields with the new contact Ids in newClass, but my test coverage is still at 84%, I am getting the following error:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_FILTER_VALIDATION_EXCEPTION, This person is not a qualified instructor. Please select a qualified instructor.: [Instructor__c]
What am I missing? any help would be much appreciated.
public class ClassSelectorController {
public List<Class__c> classes { get; set; }
String dayFormat = 'MM/DD';
public Id classId { get; set; }
public String userEmail { get; set; }
public String userProfileId;
public Profile gwCommunityProfile;
public Id classType { get; set; }
public ClassSelectorController() {
userEmail = UserInfo.getUserEmail();
userProfileId = UserInfo.getProfileId();
gwCommunityProfile = [
SELECT Id, Name
FROM Profile
WHERE Name = 'GW Community User'
LIMIT 1
];
System.debug(gwCommunityProfile);
System.debug(gwCommunityProfile.Id);
System.debug(userProfileId);
getClasses();
}
public void getClasses() {
// Limit visibility to GW community user
if (userProfileId != gwCommunityProfile.Id) {
classes = [
SELECT
id,
name,
class_nights__c,
start_date__c,
end_date__c,
status__c,
class__c,
instructor__r.email,
RecordTypeId
FROM class__c
WHERE status__c = 'Active'
];
System.debug(classes);
} else {
classes = [
SELECT
id,
name,
class_nights__c,
start_date__c,
end_date__c,
status__c,
class__c,
instructor__r.email,
RecordTypeId
FROM class__c
WHERE
status__c = 'Active'
AND (instructor__r.Email = :userEmail
OR Co_Teacher__r.Email = :userEmail
OR Teachers_Assistant__r.Email = :userEmail)
];
System.debug(classes);
}
}
public Pagereference newPage() {
Id cohortRecordTypeId = Schema.SObjectType.class__c.getRecordTypeInfosByName()
.get('Cohort')
.getRecordTypeId();
String url = '/apex/BWRoster?id=' + classId;
if (classType == cohortRecordTypeId) {
url += '&type=Cohort';
}
return new PageReference(url);
}
}
Test Class:
@isTest
public class ClassSelectorTest {
static testMethod void testGetClassesGWProfile() {
Profile gwCommunityProfile = [SELECT Id FROM Profile WHERE Name = 'GW Community User' LIMIT 1];
Contact teacher = new Contact(FirstName = 'Teacher', LastName = 'Teaher', Email = '[email protected]');
insert teacher;
Contact coTeacher = new Contact(FirstName = 'co-teacher', LastName = 'co-teacher', Email = '[email protected]');
insert coTeacher;
// Contact assistant = new Contact(FirstName = 'assistant', LastName = 'asistant', Email = '[email protected]');
// insert assistant;
// User gwCommunityUser = new User(FirstName='Teacher', LastName='Teacher', ContactId = teacher.Id, Email='[email protected]', ProfileId= gwCommunityProfile.Id);
// insert gwCommunityUser;
// System.debug(gwCommunityUser);
class__c newClass = new class__c(name = 'Test Class', Instructor__c = teacher.Id, Co_Teacher__c = coTeacher.Id,
class_nights__c = 'Monday / Wednesday', start_date__c = date.today() - 10, end_date__c = date.today() + 45);
insert newClass;
ClassSelectorController pc = new ClassSelectorController();
pc.userEmail = '[email protected]';
pc.userProfileId = gwCommunityProfile.Id;
pc.getClasses();
System.assertEquals(1, pc.classes.size());
}
@isTest
public static void testURLIsCohort(){
String CohortRecordTypeId = Schema.SObjectType.class__c.getRecordTypeInfosByName().get('Cohort').getRecordTypeId();
class__c newClass = new class__c(name = 'Test Class', RecordTypeId = CohortRecordTypeId, class_nights__c = 'Monday / Wednesday', start_date__c = date.today() - 10, end_date__c = date.today() + 45);
insert newClass;
CLassSelectorController gwClass = new ClassSelectorController();
gwClass.classId = newClass.Id;
gwClass.classType = newClass.RecordTypeId;
PageReference rosterPage = gwClass.newPage();
System.assertEquals('/apex/BWRoster?id='+newClass.Id + '&type=Cohort',rosterPage.getUrl());
}
}