我对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];
}
}
我到处搜索,尝试使用十几个不同的模板,但一直打墙。任何人都可以帮助我吗?
答案 0 :(得分:1)
为对象创建示例数据。我们建议您创建一个可以包含示例测试数据的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());
}
}
}