You need to sign in to do that
Don't have an account?
ShawnF
Help writing test methods for a controller extension
In written english, what I am trying to accomplish is the following:
- Build a visualforce page displaying details for a specific Property__c record
- For that property, show a list on the page of related child Suite__c records where Status__c = 'Available'
- For that property, show a list on the page of related child Spec_Sheet__c records where Property__c = 'the id of the property being viewed'
- Use this visualforce page publically as part of a Force.com Site (meaning, users will not authenticate)
Here is my controller extension:
public class MyPropertyExtension {
Property__c prop;
public MyPropertyExtension(ApexPages.StandardController PropertyController) {
this.prop = (Property__c)PropertyController.getRecord();
}
public List<Suite__c> getSuiteRecords() {
return [Select s.id, s.Name, s.Status__c, s.Property__c, Total_Raised_Floor_sqft__c, KVA__c, kW_of_UPS__c, Available_PDU_Capacity_kW__c from Suite__c s Where (s.Status__c!= 'Leased') AND (s.Property__c =:ApexPages.currentPage().getParameters().get('id'))];
}
public List<Spec_Sheet__c> getSpecRecords() {
return [Select s.id, s.Name, s.Property__c, s.Suite__c from Spec_Sheet__c s Where (s.Suite__r.Status__c = 'Available') AND (s.Property__c =:ApexPages.currentPage().getParameters().get('id'))];
}
}
And here is what I have as my test class:
@isTest
private class TestMyPropertyExtension {
private final Property__c prop;
public TestMyPropertyExtension(ApexPages.StandardController PropertyController) {
this.prop = (Property__c)PropertyController.getRecord();
}
static testmethod void testGetSuitesPositive(){
// Create dummy data for test purposes.
// Always assume your org has no data because the record may not be there later.
Property__c p = new Property__c();
p.Name = 'foo';
System.debug('Inserting the test property record...');
insert p;
Suite__c sAvailable = new Suite__c();
sAvailable.Name ='bar';
sAvailable.Property__c = p.Id;
sAvailable.Status__C = 'Available';
System.debug('Inserting the test suite record with status = Available...');
insert sAvailable;
Suite__c sLeased = new Suite__c();
sLeased.Name ='bar';
sLeased.Property__c = p.Id;
sLeased.Status__c = 'Leased';
System.debug('Inserting the test suite record with status = Leased...');
insert sLeased;
// Use the vf page in my org named 'locatorDetails'
PageReference pageRef = Page.locatorDetails;
Test.setCurrentPage(pageRef);
// Set the id of the current vf page to a Property__c record that I know has child Suite__c and Spec_Sheet__c records
ApexPages.currentPage().getParameters().put('id', p.Id);
TestMyPropertyExtension controller = new TestMyPropertyExtension();
//Call controller.suiteRecords() and assert the list contains the expected records.
List<Suite__c> suiteRecords = controller.suiteRecords();
system.assertEquals(suiteRecords.size(),1);
for(Suite__c s: suiteRecords)
{
System.assertEquals('Available', s.Status__c);
System.assertEquals(sAvailable.Id, s.Id);
}
}
static testmethod void testGetSpecSheetsPositive(){
// Create dummy data for test purposes.
// Always assume your org has no data because the record may not be there later.
Property__c p = new Property__c();
p.Name = 'foo';
insert p;
Suite__c sAvailable = new Suite__c();
sAvailable.Name ='bar';
sAvailable.Property__c = p.Id;
sAvailable.Status__C = 'Available';
insert sAvailable;
Spec_Sheet__c spec = new Spec_Sheet__c();
spec.Name ='bar';
spec.Property__c = p.Id;
spec.Suite__c = sAvailable.Id;
insert spec;
// Use the vf page in my org named 'locatorDetails'
PageReference pageRef = Page.locatorDetails;
Test.setCurrentPage(pageRef);
// Set the id of the current vf page to a Property__c record that I know has child Suite__c and Spec_Sheet__c records
ApexPages.currentPage().getParameters().put('id', p.Id);
TestMyPropertyExtension controller = new TestMyPropertyExtension();
//Call controller.getSpecSheetLookup() and assert the list contains the expected records.
List<Spec_Sheet__c> specRecords = controller.specRecords();
system.assertEquals(specRecords.size(),1);
}
}
The error message I receive is:
Error: Compile Error: Constructor not defined: [TestMyPropertyExtension].<Constructor>() at line 39 column 46
Which has to do with this line in my test class:
TestMyPropertyExtension controller = new TestMyPropertyExtension();
I'm not certain why it is giving me a "constructor not defined" error message when I have the constructor at the top of my test class. Here:
private final Property__c prop;
public TestMyPropertyExtension(ApexPages.StandardController PropertyController) {
this.prop = (Property__c)PropertyController.getRecord();
}
Two questions:
- Can you tell me what the "constructor not defined" error message means, so that I can learn from this?
- Can you tell me what I am doing wrong at that particular line where I am receiving the error message?
.The error message does actually describe what's wrong, but it's terse and easily missed. Note that it says:
: Constructor not defined: [TestMyPropertyExtension].<Constructor>()
The () is important. It's saying you don't have a constructor that takes no arguments. And you don't.
You have only defined a constructor that takes one argument:
public TestMyPropertyExtension(ApexPages.StandardController PropertyController) ....
So that's why you're getting the error. But besides that I think a few other things are wrong. In your test class you say:
Why do you instantiate the test class in the test class? I suspect you wanted to say:
But even that won't work of course, because that class also needs an argument in the constructor. So what I suggest you do is
a) Create an additional constructor on MyPropertyExtension for testing purposes:
b) modify your test code to call instantiate your extensino using this constructor, passing in an appropriate property.
Jon