如何为pageBlockTable控制器编写基本测试类?

时间:2015-02-24 23:20:11

标签: salesforce visualforce apex

我对Apex代码开发很陌生,并且对如何为我的基本控制器创建测试类感到困惑。这是我的控制器:

public class BuyerAlert{

    public List<Buyer__c> listOfBuyer {get; set;}
    public Buyer__c Buyer {get;set;}

    public BuyerAlert() {
        listOfBuyer = [Select id, name, Raw_Conversion__c, Call_Dur__c, Sold_Calls_Today__c from Buyer__c WHERE  Buyer_Call_Handling_Alert__c = TRUE ORDER BY Sold_Calls_Today__c DESC];
    }
}

我到处搜索,尝试使用十几个不同的模板,但一直打墙。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

  1. 创建一个单独的测试类(即BuyerAlertTest.cls)
  2. 在课程顶部添加以下注释:@isTest。 旁白:你也可以使用@ seeAllData = True,但这是不可取的。网上有无数文章讨论这个问题,这超出了这个问题的范围。
  3. 创建测试方法
  4. 为对象创建示例数据。我们建议您创建一个可以包含示例测试数据的Test Utility类,但是现在,我们只关注一个简​​单的测试类。

     @isTest
     public class BuyerAlertTest
     {
        public static testmethod void testBuyerAlertConstructor()
        {
             try
             {
               // Create sample data.
               Buyer__c buyer = new Buyer__c ();
               buyer.Name = 'Test Buyer';
               buyer.Sold_Calls_Today__c = 5;
               upsert buyer;
    
               // Instantiate your controller class now
               BuyerAlert buyerAlert = new BuyerAlert();
    
               // Verify your data with relevant predicates. You only have a constructor to test, so this is just a basic assertion
               system.assertEquals('Test Buyer', buyer.Name);
             }
             catch(Exception ex)
             {
                system.debug(ex.getMessage());
             }
        } 
    }