0

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%?

  • 1
    Not sure on how you are getting any coverage with that class. It should not even compile – Eric Feb 6 '15 at 16:22
1

You need to instantiate your extension and call actions on it. Considering your constructor does not take either ApexPages.StandardController or ApexPages.StandardSetController, this isn't really an extension, but a controller. Regardless, if you unit test your code it should look something like:

static testMethod void testGetRecent2()
{
    // set up some data
    topRecordExt extension = new topRecordExt();

    Test.startTest();
        extension.getRecent2();
    Test.stopTest();

    // make some assertions
}

static testMethod void testGetMostVoted2()
{
    // set up some data
    topRecordExt extension = new topRecordExt();

    Test.startTest();
        extension.getMostVoted2();
    Test.stopTest();

    // make some assertions
}

On a side note, you should really lazy load your queries:

public List<My_Lobby__c> recentLobbies
{
    get
    {
        if (recentLobbies == null)
        {
            recentLobies = [
                SELECT Id, Title__c, CreatedBy.Name, CreatedDate, VoteTotal__c
                FROM My_Lobby__c
                WHERE Active_Status__c = 'Active' 
                ORDER BY createdDate DESC
                LIMIT 10
            ];
        }
        return recentLobbies;
    }
    private set;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.