You need to sign in to do that
Don't have an account?

Testing a visualforce controller
I'm trying to test my visualforce controller but so far I'm just not seeing how to change example testcontrollers I see out there to fit my simple controller. My controller is just
Public with Sharing Class Courseinlineedit {
Private Final Opportunity opp;
Public CourseInLineEdit (Apexpages.StandardController Stdcontroller)
{
this.opp= (Opportunity)Stdcontroller.getrecord();
FirstCourses = [select ID, Name,Contact__c, Chapters_ish__c,First_Tech_TA_Contact__c, Course_Delivered__c ,TA_Notes__c, Key_Code_Info__c, Adoption_Agreement__c from Course__c where Opportunity__c= :this.opp.id order by Name];
}
public list<Course__c> FirstCourses{get;set;}
public pagereference updateListItems()
{
if(GlobalHelper.CheckForNotNull(FirstCourses))
{
List<database.saveresult> saveResults = database.update(FirstCourses);
}
return null;
}
}
but all I have so far for my test class is a mess that doesn't even save, n/m test what my controller is doing.
public class CourseinlineeditTests {
public static testMethod void testMyController() {
PageReference pageRef = Page.Courseinlineedit;
Test.setCurrentPage(pageRef);
thecontroller controller = new thecontroller();
String nextPage = controller.save().getUrl();
// Verify that page fails without parameters
System.assertEquals('/apex/failure?error=noParam', nextPage);
//Step 1 : Data Insertion
Account a=new Account(Name='Test Account');
insert a;
Contact c = new Contact(FirstName='John',LastName='Doe');
insert c;
Opportunity o = new Opportunity(Name='Test Opportunity',closedate=system.today(), stagename='Confirmed Teaching/Class Schedule',Probability=0.95);
insert o;
OpportunityContactRole ocr = new OpportunityContactRole (OpportunityID = o.id, ContactID=c.id, role='Decision Maker') ;
insert ocr;
Quote q= new Quote (Name='Test Quote', ContactID=c.id, OpportunityID=o.id);
insert q;
Course__c newCS = new Course__c (Adoption_Agreement__c=q.id, Opportunity__c=o.id, Contact__c=c.id);
// Add parameters to page URL
ApexPages.currentPage().getParameters().put(o.id);
// Instantiate a new controller with all parameters in the page
controller = new Courseinlineedit();
controller.setOpportunity(o.id);
nextPage = controller.save().getUrl();
// Verify that the Courseinlineedit page displays
System.assertEquals('/apex/Courseinlineedit', nextPage);
}
}
Since you are using a Controller extension you needed to create it differently. Actually this link is better than the other one. The link before shows how to test a Controller. You are actually trying to test a Controller Extension which is why you are getting that error. In order to create your controller you will need:
http://salesforcesource.blogspot.com/2008/09/testing-your-controller-extentions.html
All Answers
Your controller class name is CourseInlineEdit. You are trying to call it TheController. You need to change the name. Also, don't initialize your controller until AFTER you create your data. You were probably following the tutorial link I have posted below. If you look they actually named their class TheController, which is why the example uses that:
http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm
Since you are using a Controller extension you needed to create it differently. Actually this link is better than the other one. The link before shows how to test a Controller. You are actually trying to test a Controller Extension which is why you are getting that error. In order to create your controller you will need:
http://salesforcesource.blogspot.com/2008/09/testing-your-controller-extentions.html
Thanks. Just the basic code from that page helped get the first half covered but I'm not sure how to call the second half. This is the code that it still says isn't covered. How might I go about calling it?
public pagereference updateListItems()
{
if(GlobalHelper.CheckForNotNull(FirstCourses))
{
List<database.saveresult> saveResults = database.update(FirstCourses);
}
return null;
}
Here's my test class thus far:
public class testCourseinlineedit {
static testMethod void Courseinlineedit_Test()
{
//Test coverage for the courseinlineedit visualforce page
PageReference pageRef = Page.Courseinlineedit;
Test.setCurrentPageReference(pageRef);
Account a=new Account(Name='Test Account');
insert a;
Contact c = new Contact(FirstName='John',LastName='Doe');
insert c;
Opportunity o = new Opportunity(Name='Test Opportunity',closedate=system.today(), stagename='Confirmed Teaching/Class Schedule',Probability=0.95);
insert o;
OpportunityContactRole ocr = new OpportunityContactRole (OpportunityID = o.id, ContactID=c.id, role='Decision Maker') ;
insert ocr;
Quote q= new Quote (Name='Test Quote', ContactID=c.id, OpportunityID=o.id);
insert q;
Course__c newCS = new Course__c (Adoption_Agreement__c=q.id, Opportunity__c=o.id, Contact__c=c.id);
ApexPages.StandardController sc = new ApexPages.standardController(o);
// create an instance of the controller
Courseinlineedit CourseinlineeditCon = new Courseinlineedit(sc);
//try calling methods/properties of the controller in all possible scenarios
// to get the best coverage.
}
}
You just need to tell the controller to call that method:
Thanks,
I'm still stuck with the FirstCourses list on line List<database.saveresult> saveResults = database.update(FirstCourses);
and how to set it in the test class.
I tried the following but it's not happy with my syntax
FirstCourses = [select ID, Name,Contact__c, Chapters_ish__c,First_Tech_TA_Contact__c, Course_Delivered__c ,TA_Notes__c, Key_Code_Info__c, Adoption_Agreement__c from Course__c where Opportunity__c= :o.id order by Name];
public list<Course__c> FirstCourses{get;set;};
courseInlineEditCon.updateListItems();
List<database.saveresult> saveResults = database.update(FirstCourses);
It's because there is no variable called firstCourses, but you're trying to set it anyways.
Nope. The you are just trying to set the variable there. It has not yet been created. Create it in the same way you created your other variable on your controller.
Thanks for your help! Here's what ended up working:
public class testCourseinlineedit {
static testMethod void Courseinlineedit_Test()
{
//Test coverage for the courseinlineedit visualforce page
PageReference pageRef = Page.Courseinlineedit;
Test.setCurrentPageReference(pageRef);
Account a=new Account(Name='Test Account');
insert a;
Contact c = new Contact(FirstName='John',LastName='Doe');
insert c;
Opportunity o = new Opportunity(Name='Test Opportunity',closedate=system.today(), stagename='Confirmed Teaching/Class Schedule',Probability=0.95);
insert o;
OpportunityContactRole ocr = new OpportunityContactRole (OpportunityID = o.id, ContactID=c.id, role='Decision Maker') ;
insert ocr;
Quote q= new Quote (Name='Test Quote', ContactID=c.id, OpportunityID=o.id);
insert q;
Course__c newCS = new Course__c (Adoption_Agreement__c=q.id, Opportunity__c=o.id, Contact__c=c.id);
insert newCS;
ApexPages.StandardController sc = new ApexPages.standardController(o);
// create an instance of the controller
Courseinlineedit CourseinlineeditCon = new Courseinlineedit(sc);
//try calling methods/properties of the controller in all possible scenarios
// to get the best coverage.
courseInlineEditCon.updateListItems();
}
}