public class searchFeature { public static List<List<object>> searchRecords(string searchquery) { return [FIND :searchquery IN ALL FIELDS RETURNING Account, Opportunity, Lead]; } } // Test Class @isTest private class searchFeature_Test { @TestSetup private static void makeData() { //insert opportunities, accounts and lead } private static searchRecords_Test() { List<List<object>> records = searchFeature.searchRecords('Test'); System.assertNotEquals(records.size(),0); } } しかし、テストを実行するとデータが返されず、アサーションが失敗します。テストクラスが正常に実行されるようにするには、開発者はどのような編集を行う必要がありますか?
正解:D
When executing Salesforce Object Search Language (SOSL) queries within an Apex test context, the search engine does not actually search the database or the records created within the test setup by default. Instead, it returns an empty result set. This behavior ensures that tests run quickly and deterministically, without relying on the asynchronous indexing process of the full-text search engine. To test code that utilizes SOSL, developers must define the expected search results explicitly using the Test. setFixedSearchResults(recordIds) method. This method accepts a list of Record IDs that the FIND clause should return. By implementing setFixedSearchResults in the test method before calling the searchFeature. searchRecords method, the SOSL query will return the specified test records, allowing the System. assertNotEquals assertion to pass. Options A (startTest/stopTest) helps with governor limits but does not fix SOSL results. Option B (SeeAllData) is generally discouraged and does not guarantee SOSL indexing availability. Option C (without sharing) affects record visibility but does not force the search engine to return un-indexed test data.