You need to sign in to do that
Don't have an account?
AAlex11
Testing Controller extension
Hi, I've recently written a basic controller extension for a visualforce page, and of course now I need to test it.
Problem is I can't reach the necessary code coverage.
I need some help to solve this issue.
That's my controller code :
It would be great if someone could help me write the correct test class for this controller.
Problem is I can't reach the necessary code coverage.
I need some help to solve this issue.
That's my controller code :
public with sharing class Contact_survey_CX { private final Contact ctc; private final Contact ctc2; public Contact_survey_CX(ApexPages.StandardController stdController) { this.ctc = (Contact)stdController.getRecord(); this.ctc2= (Contact)stdController.getRecord(); } public List<Survey_Question__c> results { get { if (results == null) { results = new List<Survey_Question__c>([SELECT Name, ordernumber__c, question__c FROM Survey_Question__c WHERE Survey__r.name='Psico-Motivazionale'ORDER BY Survey_Question__c.ordernumber__c ASC]); } return results; } private set; } public List<SurveyQuestionResponse__c> results4 { get { if (results4 == null) { results4 = new List<SurveyQuestionResponse__c>([SELECT name,response__c FROM SurveyQuestionResponse__c where Survay__c='Psico-Motivazionale' AND SurveyTaker__r.Contact__r.Id= :ctc2.Id order by Name , LastModifiedDate ]); } return results4; } private set; } }
that's the test class for the controller
@isTest public class Contact_survey_CXTest{ static testMethod void testTableSurveyPage() { //data for the test Contact ct= new Contact( LastName='test1'); insert ct; Survey__c s2= new Survey__c(name='test1', Submit_response__c='test1'); insert s2; Survey__c s1=[SELECT Id FROM Survey__c limit 1]; Survey_Question__c sq1= new Survey_Question__c(Name='question_test_1', survey__c=s1.id, ordernumber__c=4000, type__c='nessuno', question__c='text_quest_1'); insert sq1; Survey_Question__c sq2=[SELECT Id FROM Survey_Question__c limit 1]; SurveyTaker__c st2= new SurveyTaker__c(taken__c='text1'); insert st2; SurveyTaker__c st1= [SELECT Id FROM SurveyTaker__c limit 1]; SurveyQuestionResponse__c sr1= new SurveyQuestionResponse__c(Survey_Question__c= sq2.id, SurveyTaker__c=st1.id); insert sr1; // create a new Contact standard controller by passing it the account record ApexPages.StandardController controller = new ApexPages.StandardController(ct); // now pass it to the extension Contact_survey_CX stdController = new Contact_survey_CX(controller); ApexPages.currentPage().getParameters().put('id',st1.id ); system.assert(stdController != null); // controller has successfully been created } }This test class pass but unfortunately works just for the first 7 rows.
It would be great if someone could help me write the correct test class for this controller.
Just want to know :
this.ctc = (Contact)stdController.getRecord();
this.ctc2= (Contact)stdController.getRecord();
Any specific reason for using 2 varibales ctc, ctc2 to hold same value? By looking into code it seems only one variable is enough.
All Answers
Just want to know :
this.ctc = (Contact)stdController.getRecord();
this.ctc2= (Contact)stdController.getRecord();
Any specific reason for using 2 varibales ctc, ctc2 to hold same value? By looking into code it seems only one variable is enough.
You're right I can use just one variable ctc,I rectify my code just now.
But I 've this problem now:
the system.assertEquals give me this error :
Comparison arguments must be compatible types: Integer, Boolean in row 39 of your code, How Can I fix this??
Any suggestions?
Thanks
Thanks anyway guys!
system.assertEquals(1, stdController.results.size());
// verify if one row returned
system.assertEquals(1, stdController.results4.size() );