You need to sign in to do that
Don't have an account?

Apex Test Class with if/else statement for profile
Hello everyone!
I am trying to reach 100% test coverage for the following code, currently it's 88%. Kindly seeking your help, thank you!
public class StudentCheckController {
public List<Class__c> classes{get; set;}
String dayFormat = 'MM/DD';
public Id classId{get; set;}
public string userEmail{get;set;}
public string userProfile;
public StudentCheckController(ApexPages.StandardController sc) {
userEmail = UserInfo.getUserEmail();
userProfile = UserInfo.getProfileId();
System.debug(userProfile);
getClasses();
}
public void getClasses() {
Id cohortRecordTypeId = Schema.SObjectType.class__c.getRecordTypeInfosByName().get('Cohort').getRecordTypeId();
System.debug(cohortRecordTypeId);
// Limit visibility to SSS community user
if(userProfile != '00a7i000000dSK9OOL') {
classes = [ SELECT
id,
name,
class_nights__c,
start_date__c,
end_date__c,
status__c,
class__c,
instructor__r.email
FROM class__c WHERE RecordTypeId = :cohortRecordTypeId AND 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
FROM class__c WHERE RecordTypeId = :cohortRecordTypeId AND status__c = 'Active' AND instructor__r.email = :userEmail];
System.debug(classes);
}
}
public Pagereference newPage() {
// Pagereference pf = new Pagereference('/apex/StudentList?id=' + classId);
Pagereference pf = new Pagereference('https://armhat.force.com/coordinatorportal/StudentList?id=' + classId);
return pf;
}
}
Test Class:
@isTest
public class StudentCheckControllerTest {
static testMethod void testGetClassesNotEqualToProfile(){
class__c newClass = new class__c(name = 'Test Class', class_nights__c = 'Monday / Wednesday', start_date__c = date.today() - 10, end_date__c = date.today() + 45);
insert newClass;
ApexPages.StandardController sc = new ApexPages.StandardController(newClass);
StudentCheckController pc = new StudentCheckController(sc);
pc.userEmail = 'test@gmail.com';
pc.newPage();
pc.userEmail = 'dreyes@armhat.com';
pc.getClasses();
}
>> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm
You need to make use of runas to run the snippet of code in as that particular profile.
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks.
Thank you for your help. But I do not know how to apply the runAs method in this particular situation, any assistance would be highly appreciated.