function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDCDenverSFDCDenver 

Unable to get Controller Test to work

Hi,
New to VF and I have a page that I am trying to test but I am not successful.  Need assistance as I don't understand why its getting that error.

Thank you.

TEST Code
  Test.startTest();
  PageReference pageRef = Page.SE_Designation_Section;
  Test.setCurrentPage(pageRef);
  ApexPages.CurrentPage().getParameters().put('Rack','Test Rack');
  DesignationEditorController controller = new DesignationEditorController();
  controller.Add();
  controller.Save();
  controller.DeleteDesignation();
  Test.stopTest();

ERROR
Compile error at line 5 column 44
Constructor not defined: [DesignationEditorController].<Constructor>()
 
VF CONTROLLER
public class DesignationEditorController {

public List<SE_Designation__c> Designations { get; set; }

private final Discovery__c ParentDiscovery;

public String Blurb { get; set; }

//used to get a hold of the record selected for deletion

public string SelectedDeleteId { get; set; }

public DesignationEditorController(ApexPages.StandardController stdController) {

this.ParentDiscovery = (Discovery__c) stdController.getRecord();

refreshDesignations();

}

public void Add() {

doSave();

SE_Designation__c newDes = initNewDesignation();

insert newDes;

refreshDesignations();

}

public void Save() {

doSave();

return;

}

private void doSave() {

update Designations;

}

private SE_Designation__c initNewDesignation() {

SE_Designation__c d = new SE_Designation__c();

d.Discovery__c = ParentDiscovery.Id;

d.Name = 'New';

return d;

}

private void refreshDesignations() {

Designations = [SELECT Id,Dest_Position__c,Dest_Time_Line__c,Dest_Pri_Power_Prod_pick__c,Dest_Pri_Receptacle__c,

Dest_Red_Power_Prod_pick__c,Dest_Red_Receptacle__c,Dest_DC_Product_pick__c,Dest_V_Drop__c,Dest_DC_Lug__c,Dest_DC_Term__c,

Dest_Cabinet_Footprint__c, Dest_DEMARC__c, Dest_Weight__c, Dest_Cab_Notes__c FROM SE_Designation__c

WHERE Discovery__c = :ParentDiscovery.Id

ORDER BY LastModifiedDate ASC NULLS FIRST

];

}

public void DeleteDesignation() {

// if for any reason we are missing the reference

if (SelectedDeleteId == null) {

return;

}

// find the record within the collection

SE_Designation__c tobeDeleted = null;

for(SE_Designation__c d : Designations)

if (d.Id == SelectedDeleteId) {

tobeDeleted = d;

break;

}

//if record found delete it

if (tobeDeleted != null) {

delete tobeDeleted;

}

//refresh the data

refreshDesignations();

}

}
Best Answer chosen by SFDCDenver
JeffreyStevensJeffreyStevens
So, in your controller - you need a constructor.  A constructor is like a method that doesn't return anything, and is named the name as your controller.

so I think the top few lines should be
 
public class DesignationEditorController {

public List<SE_Designation__c> Designations { get; set; }

private final Discovery__c ParentDiscovery;

public String Blurb { get; set; }

//used to get a hold of the record selected for deletion

public DesignationEditorController() {

public string SelectedDeleteId { get; set; }

public DesignationEditorController(ApexPages.StandardController stdController) {

this.ParentDiscovery = (Discovery__c) stdController.getRecord();

refreshDesignations();

}

}

try that instead.

All Answers

JeffreyStevensJeffreyStevens
So, in your controller - you need a constructor.  A constructor is like a method that doesn't return anything, and is named the name as your controller.

so I think the top few lines should be
 
public class DesignationEditorController {

public List<SE_Designation__c> Designations { get; set; }

private final Discovery__c ParentDiscovery;

public String Blurb { get; set; }

//used to get a hold of the record selected for deletion

public DesignationEditorController() {

public string SelectedDeleteId { get; set; }

public DesignationEditorController(ApexPages.StandardController stdController) {

this.ParentDiscovery = (Discovery__c) stdController.getRecord();

refreshDesignations();

}

}

try that instead.
This was selected as the best answer
SFDCDenverSFDCDenver
Thanks, but what I am trying to do is write the test against the controller so I get the error when executing anonymously to check test code.  The controller is working fine in the sandbox.
AdamDawAdamDaw
It looks like your controller method is taking the stdController, but then in your test you're calling it without passing the argument. I think that's the issue?
SFDCDenverSFDCDenver
So How do I write test against it?  The current docs / help I could find doesn't really explain.

Thank you
AdamDawAdamDaw
Discovery__c d = new Discovery__c([whatever values you need]);
ApexPages.StandardController sc = new ApexPages.StandardController(d);

then sc could be passed as the argument.
i.e. 
DesignationEditorController controller = new DesignationEditorController(sc);
JeffreyStevensJeffreyStevens
When you instansitate the controller, it will automatically run the constructor.

So your 
DesignationEditorController controller = new DesignationEditorController();
line will run the code in the constructor.  That's how it's tested. 
SFDCDenverSFDCDenver

Ok passed execute, trying validation and will let you know.  Thanks so much