【问题标题】:How to create a apex test class for my apex class如何为我的顶点类创建顶点测试类
【发布时间】:2019-02-27 13:10:44
【问题描述】:

这是我的第一个 apex 类,我真的不知道如何实现一个合适的测试类。 我的目标是实现 75% 的测试覆盖率。

我根据 cmets 进行了更新,但我只达到了 70 %。我不知道如何进一步改进这一点。

这就是我所做的:

Apex 类:

 public with sharing class AccountController {

@AuraEnabled
public static List<Account> findAll() {

    User userDetails =[SELECT Id, Name, Email, Profile.Name, UserRole.Name FROM User
        where Id=:userinfo.getUserId() ];

    // Theme4t is theme that is used by mobille app for  android or iphone 
    if(((userDetails.UserRole.Name).equals('yon')|| (userDetails.UserRole.Name).equals('bon')|| (userDetails.UserRole.Name).contains('non')
        || (userDetails.UserRole.Name).contains('go')) && UserInfo.getUiTheme() != 'Theme4t'){
       return [SELECT id, name, AccountStatus__c, ShippingLatitude, ShippingLongitude, ShippingCity
        FROM Account
        WHERE ShippingLatitude != NULL AND ShippingLongitude != NULL 
        LIMIT:22000];

    }else {
       return [SELECT id, name, AccountStatus__c, ShippingLatitude, ShippingLongitude, ShippingCity
        FROM Account
        WHERE OwnerId =: UserInfo.getUserId() AND ShippingLatitude != NULL AND ShippingLongitude != NULL 
        LIMIT:5000]; 

    }
}

Apex 测试类:

 @isTest 
public class AccountControllerTest 
{
static testMethod void testMethod1() 
            {
                           Account acc = new Account();
                           acc.Name='Test';

                           insert acc;
                           User userDetails =[SELECT Id, Name, Email, Profile.Name, UserRole.Name FROM User
        where Id=:userinfo.getUserId() ];


                          List<Account> lstAcc = AccountController.findAll();
                          UserRole ur =new UserRole();


                         userDetails.UserRoleId=[select Id from UserRole where Name='yon'].Id;
               System.runAs(userDetails){
                          List<Account> lstAcc1 = AccountController.findAll();  

               }

               userDetails.UserRoleId=[select Id from UserRole where Name='bon'].Id;
               System.runAs(userDetails){
                          List<Account> lstAcc2 = AccountController.findAll();  

               }

                userDetails.UserRoleId=[select Id from UserRole where Name='non'].Id;
               System.runAs(userDetails){
                          List<Account> lstAcc3 = AccountController.findAll();  

               }

               userDetails.UserRoleId=[select Id from UserRole where Name='go'].Id;
               System.runAs(userDetails){
               List<Account> lstAcc4 = AccountController.findAll();  

               }                       
}

【问题讨论】:

    标签: testing salesforce apex apex-code salesforce-lightning


    【解决方案1】:

    请完成以下线索以了解 Salesforce 中的单元测试。 https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro

    而且当您在插入帐户后尝试创建用户时,它会引发混合 DML 错误。你需要使用 system.runAs() 方法。按照以下网址使用该方法。

    https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm

    如果您需要任何帮助,请告诉我。

    这是您的课程和测试课程的代码。请遵循http://blog.shivanathd.com/2013/11/Best-Practices-Test-Class-in-Salesforce.html 的最佳实践 这次我提供代码给您了解如何创建测试类,但下次请按照我分享的步骤和文档进行操作。

    public with sharing class AccountController { 
                //using a test visible variable for setting the ui theme check.
                @TestVisible static Boolean isTheme4t = UserInfo.getUiThemeDisplayed() == 'Theme4t';
                @AuraEnabled
                public static List<Account> findAll() {
    
                    User userDetails =[SELECT Id, Name, Email, Profile.Name, UserRole.Name FROM User  where Id=:userinfo.getUserId()];
    
                    // Theme4t is theme that is used by mobille app for  android or iphone 
                    if(((userDetails.UserRole.Name).equals('yon')|| (userDetails.UserRole.Name).equals('bon')|| (userDetails.UserRole.Name).contains('non') || (userDetails.UserRole.Name).contains('go')) && !isTheme4t){
                        return [SELECT id, name, AccountStatus__c, ShippingLatitude, ShippingLongitude, ShippingCity FROM Account WHERE ShippingLatitude != NULL AND ShippingLongitude != NULL LIMIT 22000];
    
                    }else {
                        return [SELECT id, name, AccountStatus__c, ShippingLatitude, ShippingLongitude, ShippingCity FROM Account WHERE OwnerId =: UserInfo.getUserId() AND ShippingLatitude != NULL AND ShippingLongitude != NULL LIMIT 5000]; 
                    }
                }
     }
    
    
    
    
        @isTest 
        public class AccountControllerTest 
        {
            //Use setup data method to create data and query it in testmethod 
            @testSetup static void setup() {
                UserRole r = new UserRole(DeveloperName = 'yon', Name = 'yon');
                insert r;
                User u = new User(
                    ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,
                    LastName = 'last',
                    Email = 'puser000@amamama.com',
                    Username = 'puser000@amamama.com' + System.currentTimeMillis(),
                    CompanyName = 'TEST',
                    Title = 'title',
                    Alias = 'alias',
                    TimeZoneSidKey = 'America/Los_Angeles',
                    EmailEncodingKey = 'UTF-8',
                    LanguageLocaleKey = 'en_US',
                    LocaleSidKey = 'en_US',
                    UserRoleId = r.Id
                );
                insert u;
                System.runAs(u){
                    Account acc = new Account();
                    acc.Name = 'Test Account';
                    acc.ShippingLatitude = 75.46;
                    acc.ShippingLongitude = 45.46;
                    acc.AccountStatus__c = 'test';
                    insert acc;
                }
            }
    
            static testMethod void testMethod1(){
                user u = [select Id from User where email = 'puser000@amamama.com' limit 1];
                system.runAs(u){
                    Test.startTest();
                    List<Account> acc = [select Id,AccountStatus__c,ShippingLatitude,ShippingLongitude from Account where Name = 'Test Account'];
                    List<Account> lstAcc4 = AccountController.findAll();  
                    system.assert(lstAcc4.size()>0);
                    Test.stopTest();
    
                }
            }
            static testMethod void testMethod2(){
                user u = [select Id from User where email = 'puser000@amamama.com' limit 1];
                system.runAs(u){
                    AccountController.isTheme4t = true;
                    Test.startTest();
                    List<Account> acc = [select Id,AccountStatus__c,ShippingLatitude,ShippingLongitude from Account where Name = 'Test Account'];
                    List<Account> lstAcc4 = AccountController.findAll();  
                    system.assert(lstAcc4.size()>0);
                    Test.stopTest();
    
                }
            }
        }
    

    【讨论】:

    • 我用类代码和测试类代码更新了答案。了解这门课,然后从下一个开始尝试按照我分享的步骤和文档进行相同的操作。如果这对您有帮助,请将其标记为最佳答案。谢谢
    • 差不多好 72%。问题是我无法更改用户角色。可以在测试方法中更改它吗?我试过这样的东西,但没有用。用户 u = [从用户中选择 ID,其中电子邮件 = 'testuser000@amamama.com' 限制 1]; UserRole r = new UserRole(Name = 'lol');插入 r; u.UserRoleId = r.Id; runAs(u)...
    • 如果您使用我为类和测试类提供的相同代码,则为 100%。为什么要更改用户角色?它已经在那里,您的代码需要它,即“Yon”。任何一个都应该在那里。
    猜你喜欢
    • 2015-01-14
    • 2018-12-05
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多