APEX测试类0%代码覆盖率

时间:2014-07-22 05:26:43

标签: unit-testing salesforce apex-code apex

我正在尝试部署一些执行简单操作的代码,当用户单击“接受”按钮时,它会选中一个复选框(我在复选框上设置了工作流)然后我需要它将我重定向到感谢你的页面。目前我还不知道我的代码是否正确所以我需要让测试正确才能进行测试。

我的Apex课程:

public class proposalCon {
    ApexPages.StandardController stdCtrl;
    Public List <PPM_Project__c> PPM_Project_List {get;set;}

    public proposalCon(ApexPages.StandardController controller) {
        stdCtrl= controller;
        PPM_Project_List = [ select Short_Description__c from PPM_Project__c ];
    }

    public PageReference save(){

        upsert PPM_Project_List;

        PageReference reRend = new PageReference('/apex/final_approval_canvas_complete');
        reRend.setRedirect(true);
        return reRend;
    }

}

这是我的测试尝试:

@isTest
private class proposalConTest{

    static testMethod void testProposalCon() {

        // List of Message
        List <PPM_Project__c> PPM_ProjectList = new List<PPM_Project__c>();

        PPM_ProjectList.add(new PPM_Project__c (
            Name = 'A Test' ,
            Short_Description__c = 'Good Job', 
            Due_Date__c = system.today()+30,
            Final_Design_Artwork__c ='http://proteusleadership.com/DE123'
        ));

        PPM_ProjectList.add(new PPM_Project__c (
            Name = 'A Test 2' ,
            Short_Description__c = 'Good Job', 
            Due_Date__c  = system.today()+30,
            Final_Design_Artwork__c ='http://proteusleadership.com/DEf123'
        )); 

        insert PPM_ProjectList;

        Account account = new Account(Name='Test Co Pty Ltd');
        insert account;

        Contact contact = new Contact(firstName='TestFN',LastName='TestLN',email='testfn.testln@test.com',AccountId=account.Id);
        insert contact;

    // ** Start Testing ***/

        proposalCon controller = new proposalCon();

        PageReference reRend = new PageReference('/apex/final_approval_canvas_complete');
        reRend.setRedirect(true);

        PPM_ProjectList = [ select Short_Description__c from PPM_Project__c ];
    }
}

我一直在努力,没有运气,任何帮助将不胜感激。

谢谢。

1 个答案:

答案 0 :(得分:0)

您需要实例化标准控制器(为其提供PPM项目列表),然后实例化您的自定义控制器扩展 - 如下所示:

PPM_Project__c proj = new PPM_Project__c() //you may need further parameters here.

ApexPages.StandardController stdController = new apexPages.StandardController(proj);
proposalCon controller = new proposalCon (stdController); 

然后你可以保存,随心所欲地重新渲染。让我知道这是否有效 - 我还没有执行此代码,但这就是我创建自己的控制器扩展测试的方法。

这至少应该编译。但是,我想你可能真的想要一个StandardSetController。 文档在这里: SalesforceDocs

要为StandardSetController制作测试方法,请使用以下内容:

//instantiate the ApexPages.StandardSetController with an array of projects
ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(PPM_ProjectList);

//create custom controller with the StandardSetController as a param
ProposalCon ext = new ProposalCon(stdSetController);

This guy有关于如何为StandardSetController(和其他控制器)创建测试方法的更多细节