Creating a test class for a Select Statement

*爱你&永不变心* 提交于 2019-12-04 06:20:40

问题


I was wondering if you could help me. I am strugging to create a test class for the code below. Any help would be appreciated.

Many thanks

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'
      ];
   }
}

回答1:


You just need to create a test data which will be selected by your code, because the data from Org is not available in test context. After that you have to instantiate MatchReadyImage class and validate that obj has a correct value

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



回答2:


I'm confused what's the real requirement of having this class. May be you have posted very short version of it. Anyway you can use below test class(untested) for this.

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


来源:https://stackoverflow.com/questions/26840280/creating-a-test-class-for-a-select-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!