You need to sign in to do that
Don't have an account?
Btuitasi1
Help writing test class displaying data
I am trying to write a test class for my custom controller, but am only getting 30% coverage.
Controller:
Test Class so far:
Any ideas on how to get to 75%?
Controller:
public with sharing class topRecordExt { public List<My_Lobby__c> getRecent2() { return [SELECT Id, Title__c, CreatedBy.Name, CreatedDate, VoteTotal__c FROM My_Lobby__c WHERE Active_Status__c = 'Active' order by createdDate desc Limit 10]; } public List<My_Lobby__c> getMostVoted2() { return [SELECT Id, Title__c, CreatedBy.Name, CreatedDate, VoteTotal__c FROM My_Lobby__c WHERE Active_Status__c = 'Active' order by VoteTotal__c desc, CreatedDate desc Limit 10]; } public List<AggregateResult> top6Contributors1 { get { if (top6Contributors1 == null) { top6Contributors1 = [ SELECT CreatedById, CreatedBy.Name name, count(Id) contributions FROM My_Lobby__c GROUP BY CreatedById, CreatedBy.Name ORDER BY count(CreatedById) desc limit 6 ]; } return top6Contributors1; } private set; } public topRecordExt() {} // Code we will invoke on page load. public PageReference forwardToCustomAuthPage() { if(UserInfo.getUserType() == 'Guest'){ return new PageReference('MyCenter/MyCenterLogin'); } else{ return null; } } }
Test Class so far:
@isTest private class TestTopRecords { static testMethod void myUnitTest() { My_Lobby__c a = new Recent2(Title__c = 'Test', Active_Status__c = 'Active'); insert a; My_Lobby__c c = new MostVoted2(Title__c = 'Test'); insert c; My_Lobby__c d = new top6Contributors1(); return d; } }
Any ideas on how to get to 75%?
To test your controller in a Unit Test, you need to instantiate it. I actually do not know how the above Unit Test code would compile. The static void method is returning an object of type My_Lobby__c and the My_Lobby__c constructors are creating objects of type MostVoted2 and Recent2 - are these subclasses of My_Lobby__c?
So you need to do something more like this:
Please let me know how you make out.
I think the way you wrote the test class isnt the right way. Please go through the Salesforce documentation to build the test classes and it would definitely help.
For your code though , this is how it should look like