I want to cover this method :
@AuraEnabled
public static void endUserSession(Id userId){
List<AuthSession> uSessions = [ select Id , UsersId FROM AuthSession WHERE UsersId =:userId AND IsCurrent = TRUE ];
if (!uSessions.isEmpty()) {
delete uSessions;
}
}
Since we can not create AuthSession in test class , I need to mock it and to mock it I create an interface :
public interface IDatabaseWrapper {
List<SObject> queryWithBinds(
String queryString,
Map<String, Object> bindMap,
System.AccessLevel accessLevel
);
}
and class that implement the interface :
public without sharing class DatabaseWrapper implements IDatabaseWrapper {
public List<SObject> queryWithBinds(
String queryString,
Map<String, Object> bindMap,
System.AccessLevel accessLevel
) {
return Database.queryWithBinds(queryString, bindMap, accessLevel);
}
}
and here's how I modified my main method :
@TestVisible
private static IDatabaseWrapper db = new DatabaseWrapper();
@AuraEnabled
public static void endUserSession(Id userId){
List<AuthSession> uSessions = db.queryWithBinds(
'select Id , UsersId FROM AuthSession WHERE UsersId =:userId AND IsCurrent = TRUE',
new Map<String, Object>{
'userId' => userId
},
System.AccessLevel.USER_MODE
);
if (!uSessions.isEmpty()) {
delete uSessions;
}
}
But i'm blocked on how to create its test method , how to inject a mock implementation of IDatabaseWrapper