【问题标题】:Creating a test class for a Select Statement为 Select 语句创建测试类
【发布时间】:2015-01-06 13:10:36
【问题描述】:

我想知道你是否可以帮助我。我正在努力为下面的代码创建一个测试类。任何帮助将不胜感激。

非常感谢

public class MatchReadyImage {

   public Match_Day_Check_List__c obj {get; set; }


   public MatchReadyImage(){
      obj = [
         Select Id, Match_Day_Ready_Status__c
         From Match_Day_Check_List__c
         Where Name = 'Everton V West Ham United Goodison Park EPL 2013-05-12'
      ];
   }
}

【问题讨论】:

    标签: salesforce apex-code visualforce apex test-class


    【解决方案1】:

    您只需要创建一个将由您的代码选择的测试数据,因为来自 Org 的数据在测试上下文中不可用。之后,您必须实例化 MatchReadyImage 类并验证 obj 具有正确的值

    @isTest
    private class MatchReadyImageTest {
    
        @isTest
        private static void test1() {
            Match_Day_Check_List__c mdckl = new Match_Day_Check_List__c(
                name = 'Everton V West Ham United Goodison Park EPL 2013-05-12';
                // other required fields 
            );
            insert mdckl;
            // you can add assertions which you want 
            System.assert((new MatchReadyImage).obj != null);
        }
    }
    

    【讨论】:

    • 您好,感谢您的回复。我在第 13 行第 46 列收到错误“意外令牌:')'”
    【解决方案2】:

    我很困惑开设这门课的真正要求是什么。可能是您发布了非常简短的版本。无论如何,您可以为此使用以下测试类(未经测试)。

    @isTest
    private class TestMatchReadyImage {
    
        @isTest
        static testMethod void testConstructor() {
            Match_Day_Check_List__c mdckl = new Match_Day_Check_List__c()            
            mdckl.Name = 'Everton V West Ham United Goodison Park EPL 2013-05-12';
            // populate if any other fields you need to
            insert mdckl;
    
            // make assertions for the unit test
            System.assert((new MatchReadyImage()).obj != null);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-21
      • 2021-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      • 2021-09-05
      • 2011-03-31
      相关资源
      最近更新 更多