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

卡罗琳·兰姆

卡在这里写一个测试类,请建议一个测试类,下面写的测试类没有解决目的

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

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

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

                    
                 
                    
                }

提前致谢 卡罗琳

眼霜

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

试试这个(你把这个类重命名为“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');
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 salesforce apex 类中调用参数化方法

如何编写用于导入 CSV 文件的 Apex 测试类?

Salesforce Lightning Aura 组件错误

Salesforce Apex响应标题

如何为使用网络连接的类编写jUnit测试

如何为抽象类方法编写测试用例

如何为类型化的类编写单元测试?

如何为使用硬件资源的类编写单元测试?

Rspec-如何为类方法编写测试

如何为android方法和类编写单元测试?

如何为Thread类编写Junit测试用例

我如何为以下顶点代码编写测试类

如何在 Salesforce Apex 中添加日期时间?

如何将文件从Salesforce Apex上载到IBM Watson

如何通过 Salesforce Apex 中的 Web 服务发送 Twilio SMS

Apex 类和 Apex 触发器在 Salesforce 开发人员控制台中不可见

困惑于如何为重构为其他类的类编写测试

如何使我的Apex类返回或“运行” JSON?使用APEX REST

我正在为 Apex 批处理编写测试类,但测试覆盖率只有 35%

如何在 Salesforce 中执行“上次修改”日期的测试类覆盖率?

如何为特定的类编写hashCode方法?

如何为Android片段类编写unittest?

如何为类编写“获取”方法模板

我如何为 SimpleCursorAdapter 编写公共类?

如何防止MINIFY_ENABLED删除类?

如何为实现ApplicationListener <ApplicationPreparedEvent>的类编写JUNIT测试用例

如何为使用新的内部函数创建对象的Java类编写junit测试?

如何为直到键盘中断运行且不返回任何内容的类编写单元测试?

如何为任意类的数组的类编写文字