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
JHAJHA 

How to write test method?

public class MileageUtil {

static final Integer MAX_MILES_PER_DAY = 500;
public static void areMilesAllowed(accelerantforce__Mileage__c[] miles) {
Double totalMiles = 0;
String createdbyId = UserInfo.getUserId();
 
for(accelerantforce__Mileage__c mq:[SELECT miles__c FROM accelerantforce__Mileage__c
WHERE Date__c = TODAY
AND accelerantforce__Mileage__C.createdById = :createdbyId
AND accelerantforce__miles__c != null]) {
totalMiles += mq.miles__c;
}
 
for (accelerantforce__Mileage__c m:miles) {
totalMiles += m.miles__c;
if(totalMiles > MAX_MILES_PER_DAY)
m.addError('Mileage request exceeds daily limit: ' + MAX_MILES_PER_DAY);
}
}
}

Anyone can plz help me out to write test method for above code?

 

Thanks.

SteveBowerSteveBower

public class MileageUtil { static final Integer MAX_MILES_PER_DAY = 500; public static void areMilesAllowed(accelerantforce__Mileage__c[] miles) { Double totalMiles = 0; totalMiles = [select sum(miles__c) from accelerantforce__Mileage__c where Date__c = TODAY AND accelerantforce__Mileage__C.createdById = :UserInfo.getUserId() ]; for (accelerantforce__Mileage__c m:miles) totalMiles += m.miles__c; if(totalMiles > MAX_MILES_PER_DAY) m.addError('Mileage request exceeds daily limit: ' + MAX_MILES_PER_DAY); }

 

This might be better.  Best, Steve.