The documentation on preparing tests for code with SOQL queries includes the comment "Additionally, the test method can call Test.setFixedSearchResults
multiple times to define different result sets for different SOSL queries."
I'm wondering how to interpret that. It does not appear to queue results. I tried setting two and then issuing two SOSL queries and both got the latest result. I wonder if it's per object, i.e. you can set up to one result per object and if the query asks for that object, you get the result for that object. The code below returns the second list with two entries in both cases.
@isTest private class Test_SoslMulti {
private static String EMAIL1 = '[email protected]';
private static String EMAIL2 = '[email protected]';
private static String EMAIL3 = '[email protected]';
private static String STATE1 = 'MA';
private static String STATE2 = 'NH';
private static String STATE3 = 'VT';
@testSetup static void setup() {
list<Lead>leadList = new list<Lead>();
leadList.add(new Lead(LastName='Test Lead 1', State=STATE1, Company='Test Company', Status='Open - Not Contacted', Email=EMAIL1));
leadList.add(new Lead(LastName='Test Lead 2', State=STATE2, Company='Test Company', Status='Open - Not Contacted', Email=EMAIL2));
leadList.add(new Lead(LastName='Test Lead 3', State=STATE3, Company='Test Company', Status='Open - Not Contacted', Email=EMAIL3));
insert leadList;
}
static testMethod void myUnitTest() {
list<Id>idList1 = new list<Id>();
list<Id>idList2 = new list<Id>();
idList1.add([select Id from Lead where State = :STATE1].Id);
idList2.add([select Id from Lead where State = :STATE2].Id);
idList2.add([select Id from Lead where State = :STATE1].Id);
Test.setFixedSearchResults(idList1);
Test.setFixedSearchResults(idList2);
String safeValue = 'Test Lead';
String searchQuery='FIND \'' + safeValue + '\' IN ALL FIELDS RETURNING Lead (Id, Name)';
System.debug('searchQuery -- ' + searchQuery);
List<List<SObject>>sObjectListList = search.query(searchquery);
if (!sObjectListList.isEmpty()) {
List<Lead>lead1List = sObjectListList[0];
System.debug('lead1List' + lead1List);
}
sObjectListList = search.query(searchquery);
if (!sObjectListList.isEmpty()) {
List<Lead>lead2List = sObjectListList[0];
System.debug('lead2List' + lead2List);
}
}
}