Skip to main content
Tweeted twitter.com/StackSalesforce/status/1405314282288463884
fixed a type + added link to the official docs🍻
Source Link

The documentation on preparing tests for code with SOQL queriesThe documentation on preparing tests for code with SOSL queries includes the comment "Additionally, the test method can call Test.setFixedSearchResults multiple times to define different result sets for different SOSL queries."

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."

The documentation on preparing tests for code with SOSL queries includes the comment "Additionally, the test method can call Test.setFixedSearchResults multiple times to define different result sets for different SOSL queries."

Capitalization fixes, forcing syntax highlighting
Source Link
Derek F
  • 64.1k
  • 15
  • 55
  • 104

The documentation on preparing tests for code with SOQL queries includes the comment "Additionally, the test method can call Test.setFixedSearchResultsTest.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 uptoup 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);
        }        
    
    }
    
}
@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);
        }        
    
    }
    
}

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 upto 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);
        }        
    
    }
    
}

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);
        }        
    
    }
    
}

The documentation on preparing tests for code with SOQL queryeiesqueries 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. ItIt does not appear to queue results. II tried setting two and then issuing two SOSL queries and both got the latest result. II wonder itif it's per object, i.e., you can set up toupto one result per object and if the query asks for that object, you get the result for that object. TheThe code below returns the second list with two entries in both cases.

The documentation on preparing tests for code with SOQL queryeies 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 it 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.

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 upto 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.

added 183 characters in body
Source Link
sfdcfox
  • 501.8k
  • 21
  • 473
  • 828
Loading
Source Link
Ken
  • 605
  • 5
  • 16
Loading