1

I need to test the following SOSL query:

FIND {"Example"} IN ALL Fields RETURNING ExampleObject(Field_One__c, 
Field_Two__c WHERE Condition_Field != 'value')

My code behaves differently depending on retrieved values of Field_One__c and Field_Two__c. Is there a possibility to mock via Test.setFixedSearchResults() something more than a single Id per record? (I need to mock string values) If not - is it possible to do it in another way?

1 Answer 1

0

I couldn't verified if Test.setFixedSearchResults() allows to mock something more than Id, but I have managed to bypass the problem via following code:

List<List<sObject>> queryResult = Search.query(query);   // query = SOSL query from question. Inside unit test Search.query() will return empty list

if(Test.isRunningTest()) {                               // section for mocking SOSL results
        List<ExampleObject__c> matchedBySosl;
        String condition = getCondition();

        // I have added External_Id__c custom field by which I can retrieve mocking results

        if(condition == 'A') {
            matchedBySosl = [SELECT Field_One__c, Field_Two__c Id FROM ExampleObject__c WHERE External_Id__c IN ('1','2','3','4','5')];
        }
        else if(condition == 'B') {
            matchedBySosl = [SELECT Field_One__c, Field_Two__c Id FROM ExampleObject__c WHERE External_Id__c IN ('3','6')];
        }
        queryResult = new List<List<sObject>>();
        queryResult.add(matchedBySosl);
    }

    // Handling queryResult...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .