枚举的测试代码

时间:2015-04-07 06:57:52

标签: enums apex

如果我以这种方式在课堂上声明2个枚举:

    public class EnumerationExample {

    public enum Season {WINTER,SPRING,SUMMER,FALL}
    public enum Month {JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC}
    public List<Month> listMonths;
    public Month convert (String val) {

        for (Month mtObj : Month.values()) {

            if (mtObj.name() == val) {
                system.debug('The value passed is ' +mtObj);
            }
        }
    return null;
    }
    public List<Month> seasonMonths(Season s) {

        Season seasonObj = Season.SPRING;

        listMonths = new List<Month>();
        if(s==season.WINTER) {
            listMonths.add(Month.DEC);
            listMonths.add(Month.JAN);
            listMonths.add(Month.FEB);
        }
        else if(s==season.SPRING) {
            listMonths.add(Month.MAR);
            listMonths.add(Month.APR);
            listMonths.add(Month.MAY);
        }
        else if(s==season.SUMMER) {
            listMonths.add(Month.JUN);
            listMonths.add(Month.JUL);
            listMonths.add(Month.AUG);
        }
        else if(s==season.FALL) {
            listMonths.add(Month.SEP);
            listMonths.add(Month.OCT);
            listMonths.add(Month.NOV);
        }
    return listMonths;
    }
}

我该如何为此编写测试代码?

我尝试这样做,但它说季节变量不存在于EnumerationExampleObj.seasonMonths(Season.WINTER); ...

 @isTest
 public class TestEnumerationExample {
   public static testMethod void myUnitTest() {
    EnumerationExample EnumerationExampleObj = new EnumerationExample();
EnumerationExampleObj.convert('wintery');
  EnumerationExampleObj.seasonMonths(Season.WINTER);
    system.assertEquals(EnumerationExampleObj.listMonths.get(0) , Month.DEC );
}}

访问修饰符或任何特定注释是否有任何问题。?

1 个答案:

答案 0 :(得分:1)

您的问题根本与测试无关,而是与范围和语法等C#基础知识有关(您的示例代码充满了语法错误)。

回答您的具体问题:如果在类中定义公共枚举,则在该类外部使用时,必须在其前面加上类名。例如:

var enumerationExampleObj = new EnumerationExample(); 
enumerationExampleObj.seasonMonths(EnumerationExample.Season.WINTER);