function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Btuitasi1Btuitasi1 

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:

    
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%?
Brenda S FinnBrenda S Finn
Hello. I am wondering how you are getting even 30% for your Controller when you do not invoke/create the controller in the Unit Test at all. A few questions first. What line(s) are you not getting code coverage on in your controller?

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:
TopRecordExt topRecordExt = new TopRecordExt();
// set up several My_Lobby__c objects with Active_Status__c set to Status.
// Need to Set VoteTotal__c field too as it is used in getMostVoted2.

List<My_Lobby__c> lstMostVoted = topRecordExt.getMostVoted2();
// verify that return List contains your My_Lobby__c object

List<My_Lobby__c> lstRecent = topRecordExt.getRecent2();
// verify that return List contains your My_Lobby__c object

// do the same for top6Contributors.

Please let me know how you make out.


 
logontokartiklogontokartik
Hello ,
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
 
@isTest
private class TestTopRecords {
 
 static testmethod void toprecords_ut1(){
    // Insert the test data first, would be voice total and my lobby records

    Test.startTest();
       topRecordExt testExt = new topRecordExt();
       List<Lobby__c> recent1 = testExt.getRecent2();
       // assert what you are expecting
List<Lobby__c> mostvoted = testExt.getMostVoted2();
// assert what you are expecting
List<AggregateResult> topcontributes = testExt.top6Contributors1;
//assert
PageReference expectedPage = textExt.forwardToCustomAuthPage();
        
    Test.stopTest();
 }
}