You need to sign in to do that
Don't have an account?
syric
Help increasing code coverage for controller
Hello- I'm new to visualforce and this is my first project. Through the online guides and examples I have been able to accomplish almost everything. I'm trying to increase my code coverage. It is currently at 66%.
This is my controller:
This is my test class:
I can tell from the test coverage highlights that the case "MasterRecord" and the list "ChildRecords" from the controller are not covered. I assume that I would need to assert that MasterRecord = the case, and that ChildRecords ParentId__c = MasterRecord Id. I do not know how to do that. I do not know how to reference them in my test class or if that is right. Any help would be appreciated.
This is my controller:
public with sharing class CommentsController { private final Case cas; // public String MasterRecordId {get; set;} public List<Case_Comments__c> ChildRecords {get; set;} public Case MasterRecord {get; set;} //SET UP PAGE public CommentsController(ApexPages.StandardController stdController) { this.cas = (Case)stdController.getRecord(); MasterRecordId = ApexPages.currentPage().getParameters().get('id'); //CHECK FOR ID if(!String.isBlank(MasterRecordId)){ //GET PARENT MasterRecord = [ SELECT Id, CaseNumber FROM Case WHERE Id = :MasterRecordId ]; //GET CHILD RECORDS ChildRecords = [ SELECT Name, Id, CreatedDate, CreatedBy.Id, CreatedBy.Name, Time_Spent__c, Comment__c, ParentId__c FROM Case_Comments__c WHERE ParentId__c = :MasterRecordId order by CreatedDate desc ]; } } }
This is my test class:
@isTest public class testCommentsController { static testMethod void testFirstTest() { // CREATE ACCOUNT Account newAccount = new Account(Name = 'Test Account', Phone='817-999-9999'); insert newAccount; // CREATE CONTACT Contact newContact = new Contact(LastName = 'Test Contact', AccountId = newAccount.Id); insert newContact; // CREATE CASE Case newCase = new Case(Subject = 'Case Subject', Description = 'Case Description', ContactId = newContact.Id, AccountId = newAccount.Id); insert newCase; // CREATE CASE COMMENT Case_Comments__c newComment = new Case_Comments__c(ParentId__c = newCase.Id, Comment__c = 'Test Comment Body', Time_Spent__c = .5); insert newComment; ApexPages.StandardController sc = new ApexPages.StandardController(newCase); CommentsController cc = new CommentsController (sc); PageReference pageRef = Page.Comments; pageRef.getParameters().put('id', String.valueOf(newCase.Id)); Test.setCurrentPage(pageRef); } }
I can tell from the test coverage highlights that the case "MasterRecord" and the list "ChildRecords" from the controller are not covered. I assume that I would need to assert that MasterRecord = the case, and that ChildRecords ParentId__c = MasterRecord Id. I do not know how to do that. I do not know how to reference them in my test class or if that is right. Any help would be appreciated.
All Answers
Something like:
and I was getting a "Method does not exist or incorrect signature" error, but I understand now. Thanks again for the help.