I'm confused how Salesforce calculates test coverage. I've been struggling to get my app up to the 75% threshold. I have a number of files where I assume coverage should be 100%, but Salesforce says I only have 51% coverage. For instance, here is a class that only has one function, and I test this function, so I would expect to get 100% coverage. But Salesforce says I only have 51% coverage on this file. What do I need to do to get this coverage higher?
I'll shorten this file slightly to make it readable, but I have a class that has just one static function, and this is what it looks like:
/*
*
* type_of_permission should be either "select" or "insert" or "update", so you would call
* using one of these 3 arguments:
*
* SameDayPermissionModel.checkPermissions('select')
*
* SameDayPermissionModel.checkPermissions('insert')
*
* SameDayPermissionModel.checkPermissions('update')
*
*/
public static boolean checkPermissions(String type_of_permission) {
boolean permitted = true;
if ( type_of_permission != 'select' && type_of_permission != 'insert' && type_of_permission != 'update' && type_of_permission != 'delete' ) {
return false;
}
if (type_of_permission == 'select') {
if (!Schema.sObjectType.FastCash__SameDaySummaryForProfileImport__c.isAccessible()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__SameDaySession__c.isAccessible()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__Search__c.isAccessible()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__SameDayNote__c.isAccessible()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__SameDayImport__c.isAccessible()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__SameDay_Flash_Message__c.isAccessible()) {
permitted = false;
}
}
if (type_of_permission == 'insert') {
if (!Schema.sObjectType.FastCash__SameDaySummaryForProfileImport__c.isCreateable()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__SameDaySession__c.isCreateable()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__Search__c.isCreateable()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__SameDayNote__c.isCreateable()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__SameDayImport__c.isCreateable()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__SameDay_Flash_Message__c.isCreateable()) {
permitted = false;
}
}
if (type_of_permission == 'update') {
if (!Schema.sObjectType.FastCash__SameDaySummaryForProfileImport__c.isUpdateable()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__SameDaySession__c.isUpdateable()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__Search__c.isUpdateable()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__SameDayNote__c.isUpdateable()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__SameDayImport__c.isUpdateable()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__SameDay_Flash_Message__c.isUpdateable()) {
permitted = false;
}
}
/*
* 2017-08-09 -- I'm fairly sure that the only place we delete anything is in the unit tests,
* where we create a test object, insert it, then delete it.
*
*
*/
if (type_of_permission == 'delete') {
if (!Schema.sObjectType.FastCash__SameDaySummaryForProfileImport__c.isDeletable()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__SameDaySession__c.isDeletable()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__Search__c.isDeletable()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__SameDayNote__c.isDeletable()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__SameDayImport__c.isDeletable()) {
permitted = false;
}
if (!Schema.sObjectType.FastCash__SameDay_Flash_Message__c.isDeletable()) {
permitted = false;
}
}
return permitted;
}
And then I have these tests in place:
@isTest
static void testCheckPermissionsUpdate(){
// Perform test logic
Test.startTest();
boolean wasThisAllowed = SameDayPermissionModel.checkPermissions('update');
Test.stopTest();
System.assertEquals(true, wasThisAllowed, 'SameDayPermissionModel.checkPermissions(select) should return true -- if this is not true, then that means the admin who is installing this app has restricted the permissions to a point that the SameDay Search Tool will not run correctly, in which case it is appropriate that this test should fail.');
}
@isTest
static void testCheckPermissionsIncorrect(){
// Perform test logic
Test.startTest();
boolean wasThisAllowed = SameDayPermissionModel.checkPermissions('an incorrect string');
Test.stopTest();
System.assertEquals(false, wasThisAllowed, 'SameDayPermissionModel.checkPermissions() takes either select, insert, or update as a string and then tests either isAccessbile() or isCreateable() or isUpdateable(). If some other string, besides those 3, is given as an argument, then the return value is false.');
}
I have tests for all four allowed inputs, but I cut those for the sake of brevity.
All tests are passing.
Can anyone tell me why Salesforce does not consider this 100% coverage? Why is this only 51% coverage? What else do I need to do to get 100%?
[ [ UPDATE ] ]
Robert Watson pointed me to the coverage report. I now see that Salesforce does not feel that these lines are tested:
if (!Schema.sObjectType.FastCash__SameDaySummaryForProfileImport__c.isAccessible()) {
permitted = false;
}
But as near as I can see, these lines are tested, they are testing to see if a custom object has a permission.