0
votes

I am trying to deploy some code that does something simple, when the user clicks on the accept button, it checks a checkbox (I have a workflow set up on the checkbox) and then I need it to redirect me to a thank you page. At the moment I don't know if my code is correct so I need to get the test correct to test it.

My Apex class:

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

}

And here is my test attempt:

@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='[email protected]',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 ];
    }
}

I have been trying with no luck and any help would be greatly appreciated.

Thank you.

Joe

1
Is this meant to be a custom controller or a controller extension?Jagular
It is a controller extension. I am also using the standard controller PPM_Project__c. Thank you.proteuscanvas

1 Answers

1
votes

You need to instantiate a Standard Controller (feeding it a list of PPM Projects) and then instantiate your custom controller extension - like this:

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

Then you can save, rerender as you like. Let me know if this works - I haven't executed this code, but this is how I create my own controller extension tests.

This should at least compile. However, I think you may really want a StandardSetController. The docs are here: SalesforceDocs

To make a testmethod for the StandardSetController, use something like this:

//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 has more details on how to create a test method for a StandardSetController (and other controllers)