如何为 Salesforce Apex Aura Enabled class 编写测试 class?

How to Write the Test class for Salesforce Apex Aura Enabled class?

卡在这里写一个测试class,请推荐一个测试class,下面写的测试class没有解决目的

                @isTest
                public class testStack {
                    public static  testMethod void testCallClass(){

                     Account acc = new Account(Name='Test Account');
                                    insert acc;
                                    Test.startTest();
                                    LookupSearchResult lcr = new LookupSearchResult(acc.id,'Account','Test Icon','Test Title','Test Sub-Title');
                                    system.assert(lcr.getId() == acc.id);
                                    system.assert(lcr.getSObjectType() == 'Account');
                                    system.assert(lcr.getIcon() == 'Test Icon');
                                    system.assert(lcr.getTitle() == 'Test Title'); 
                                    system.assert(lcr.getSubtitle() == 'Test Sub-Title'); 
                                    Case_Type_Data__c ctd = new Case_Type_Data__c(Name='500J000000Mo18fIAB',Level_1__c='Acct management issues',Level_2__c='Issue1',Level_3__c='Issue4');
                                    insert ctd;
                                 List < List < SObject > > searchResults = Stack.search( '500J000000Mo18fIAB',acc.id );  
                        List < Case_Type_Data__c > listctd = searchResults.get( 0 );  
                        system.assertEquals( 0, listctd.size() );
                            
                                    Test.stopTest();
                    }
                }

这是启用的光环 class 需要测试 class

        public class Stack {
            @AuraEnabled(cacheable=true)
            public static List<LookupSearchResult> search(String searchTerm, List<String> selectedIds){
                if(String.isBlank(searchTerm) || searchTerm.length() < 2){
                    return null;
                }
                String t = '%' + searchTerm + '%'; // decide how you want to search, "starts with", "includes" or what
                
                List<Case_Type_Data__c> records = [SELECT Id, Name, Level_1__c, Level_2__c, Level_3__c
                    FROM Case_Type_Data__c
                    WHERE Level_1__c LIKE :t OR Level_2__c LIKE :t OR Level_3__c LIKE :t
                    ORDER BY Level_1__c, Level_2__c, Level_3__c
                    LIMIT 20];
                
                /* You could also experiment with SOSL?
                records =  [FIND :('*' + searchTerm + '*') IN ALL FIELDS 
                    RETURNING Case_Type_Data__c(Id, Name, Level_1__c, Level_2__c, Level_3__c)][0];
                */
                
                List<LookupSearchResult> results = new List<LookupSearchResult>();
                for(Case_Type_Data__c ctd : records){
                    results.add(new LookupSearchResult(ctd.Id, 'Case_Type_Data__c', 'standard:case_wrap_up', ctd.Name,
                        String.join(new List<String>{ctd.Level_1__c , ctd.Level_2__c, ctd.Level_3__c}, '; ')
                    ));
                }
                return results;
            } 

        }

之前为 PickListHandler 编写的测试,测试所有级别,即 level1 level2 和 level3

                @IsTest
                public class testGetAllLevels { 

                @IsTest
                static  void testGetLevel1()
                {
                    Case_Type_Data__c obj = new Case_Type_Data__c();
                    obj.Level_1__c = 'Test Level 1';
                    insert obj;
                    List<String> s = PickListHandler.getLevel1();

                }

                @IsTest
                static void testGetLevel2()
                {
                    Case_Type_Data__c obj = new Case_Type_Data__c();
                    obj.Level_1__c = 'Test Level 1';
                    insert obj;
                    List<String> s = PickListHandler.getLevel2('Test Level 1');

                }
                    
                @IsTest
                static void testGetLevel3()
                {
                    Case_Type_Data__c obj = new Case_Type_Data__c();
                    obj.Level_1__c = 'Test Level 1';
                    obj.Level_2__c = 'Test Level 2';
                    obj.Level_3__c = 'Test Level 3';
                    insert obj;
                    List<String> s = PickListHandler.getLevel3('Test Level 1','Test Level 2');

                }
                    
                @IsTest 
                static  void testsaveCaseType(){
                        // Create the Case Record.
                        Case cas = new Case(Status ='New', Priority = 'Medium', Origin = 'Email'); 
                        insert cas;
                       
                        ERT_Case_Type__c obj=new ERT_Case_Type__c();
                        string one='one';
                        string two='two';
                        string three='three';
                        test.startTest();
                        String testing=PickListHandler.savecasetype(one,two,three,cas.id);
                        test.stopTest();
                    }
                    

                    
                 
                    
                }

提前致谢 卡罗琳

你那里的测试质量很低,并没有真正检查搜索是否运行正常。它是作为最低限度的努力编写的,只是为了获得所需的代码覆盖率。

试试这个(您将 class 重命名为“Stack”,对吗?没关系。我将问题编号添加到它们中,否则我会发疯的;))

@isTest
public with sharing class Stack64348072Test {

    @isTest
    static void testSearch(){
        insert new List<Case_Type_Data__c>{
          new Case_Type_Data__c(Level_1__c = 'AAA', Level_2__c = 'BBB', Level_3__c = 'CCC'),
          new Case_Type_Data__c(Level_1__c = 'BBB', Level_2__c = 'BBB', Level_3__c = 'CCC'),
          new Case_Type_Data__c(Level_1__c = 'BBB', Level_2__c = 'BBB', Level_3__c = 'BBB'),
          new Case_Type_Data__c(Level_1__c = 'lvl 1', Level_2__c = 'lvl 2', Level_3__c = 'lvl 3'),
          new Case_Type_Data__c(Level_1__c = 'Some longer phrase', Level_2__c = 'to test if middle of the word', Level_3__c = 'works OK too')
        };
            
        Test.startTest();
        // First some negative test cases
        List<LookupSearchResult> result = Stack64129038.search(null, null);
        System.assertEquals(null, result, 'If nothing was sent - no results will be returned');
        
        result = Stack64129038.search('a', null);
        System.assertEquals(null, result, 'We need at least 2 characters to run the search');
        
        result = Stack64129038.search('Some unexpected text', null);
        System.assertEquals(0, result.size(), 'This phrase is not in the reference data we created so we expect no hits.');
        
        // And now some positive cases
        result = Stack64129038.search('AA', null);
        System.assertEquals(1, result.size(), 'There should be exactly 1 match');
        System.assertEquals('AAA; BBB; CCC', result[0].getSubtitle(), 'The "subtitle" should be composed of all 3 levels');
        
        result = Stack64129038.search('BB', null);
        System.assertEquals(3, result.size(), 'There should be 3 matches');
        
        result = Stack64129038.search('middle', null);
        System.assertEquals(1, result.size(), 'There should be 1 match');
        System.assertEquals('Some longer phrase; to test if middle of the word; works OK too', result[0].getSubtitle(), 'The "subtitle" should be composed of all 3 levels');
    }
}