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
Dave BerenatoDave Berenato 

Test Class for Count Function

This might be really simple, but I'm banging my head over this Test Class for an custom controller Apex Class.

A snippet of the Apex Class:
public class GoalChart {
    public Integer getAryCalls(){return (Integer) [SELECT count() FROM Task WHERE skyplatform__Is_Call_Record__c = TRUE And CreatedDate = THIS_MONTH And CreatedById = '0056A000001IVgU'];} 

}

Is this close to the right markup for the TestClass? I'm getting so many "variable does not exist" errors that I can't even know if I'm headed in the right direction.
@isTest
public class GoalChartTest{
    
    GoalChart gc = new GoalChart();
    
    static TestMethod TestAryCalls{
    system.assert(0,gc.AryCalls)
    }
 }
Any help would be greatly appreciated. Thank you.

 
Best Answer chosen by Dave Berenato
Nayana KNayana K
@isTest
public class GoalChartTest{  
    
    static TestMethod void TestAryCalls(){
GoalChart gc = new GoalChart();
    system.assertEquals(0,gc.getAryCalls());
    }
}

 

All Answers

Nayana KNayana K
@isTest
public class GoalChartTest{
    
    GoalChart gc = new GoalChart();
    
    static TestMethod TestAryCalls{
    system.assertEquals(0,gc.getAryCalls());
    }


You have hard-coded createdById in the query.  Even if you are able to successfully deploy the code,  getArrayCalls() going to return 0 records(because production user ids will be different)  unless this sandbox is full copy ( both prod and sand will have same ID)

As a best practice,  you should never hardcode ids.
Atleast if you assume user name is the main key to avoid this issue  or you are querying for the current logged in user, code can be changed like this :

IF by particular user name, 
public class GoalChart { public Integer getAryCalls(){
List <User> lstUser = [ SELECT Id FROM User WHERE name = 'Nayana K'];
if(!lstUser.isEmpty())
return (Integer) [SELECT count() FROM Task WHERE skyplatform__Is_Call_Record__c = TRUE And CreatedDate = THIS_MONTH And CreatedById = lstUser[0].Id];
return 0;
} }

IF by logged in user, 

public class GoalChart { public Integer getAryCalls(){return (Integer) [SELECT count() FROM Task WHERE skyplatform__Is_Call_Record__c = TRUE And CreatedDate = THIS_MONTH And CreatedById = userInfo.getUserId()];} }


 
Nayana KNayana K
Also test class has to be changed a bit accordingly 
Dave BerenatoDave Berenato
Sandbox is a full copy, so I'm not worried about hardcoding the IDs. I'm unable to deploy the code because I can't get a successful Test Class through. As soon as I add the "void" that I keep seeing to line 6 I get 10 new errors like:

1. Invalid constructor name system.assertEquals
2. Invalid constructor name gc.getAryCalls
 
@isTest
public class GoalChartTest{  
    GoalChart gc = new GoalChart();
    static TestMethod void TestAryCalls{
    system.assertEquals(0,gc.getAryCalls());
    }
}

 
Nayana KNayana K
@isTest
public class GoalChartTest{  
    GoalChart gc = new GoalChart();
    static TestMethod void TestAryCalls(){
    system.assertEquals(0,gc.getAryCalls());
    }
}

 
Dave BerenatoDave Berenato
New error is "Variable does not exist: gc"

I appreciate you staying to help me with this!
Nayana KNayana K
@isTest
public class GoalChartTest{  
    
    static TestMethod void TestAryCalls(){
GoalChart gc = new GoalChart();
    system.assertEquals(0,gc.getAryCalls());
    }
}

 
This was selected as the best answer
Dave BerenatoDave Berenato
You are my hero!
Nayana KNayana K
:) Thank you ;)