Jhony
Jhony

Reputation: 61

Guys I don't understand how to write tests

I have a class in which I get the number of ids per year with a soql query on a custom object:

public static Integer numberRecords(Integer year) {
        List<AggregateResult> numbersOfRecords = [
            SELECT Keeper__c
            FROM Month_Expense_Application__c
            WHERE calendar_year(MonthDate__c) =: year
        ];
        Set<Id> keepers = new Set<Id>();
        for (AggregateResult result : numbersOfRecords) {
            keepers.add((Id)result.get('Keeper__c'));
        }
        Integer value = keepers.size();
        return value;
    }

I'm trying to write a test for this method by creating an object and filling in the fields it needs, then I try to get the id with a soql request and compare:

@isTest
public class ExpenseAdminControllerTests {
    @isTest
    public static void numberRecordsTests() {
        Month_Expense_Application__c monthExpenseTest = new Month_Expense_Application__c(
            Balance__c = 12.3,
            Income__c = 11,
            Keeper__c = '0034x00001K7kGCAAZ',
            MonthDate__c = date.newInstance(2022, 11, 21)
        );
        Integer year = 2022;
        List<Month_Expense_Application__c> numbersOfRecords = [
            SELECT Keeper__c
            FROM Month_Expense_Application__c
            WHERE calendar_year(MonthDate__c) =: year AND Id =: monthExpenseTest.Id
        ];
        Set<Id> keepers = new Set<Id>();
        keepers.add(numbersOfRecords[0].Keeper__c);
        Integer value = keepers.size();
        System.assertEquals(1, value);
    }
}

But i cant tell me what am i doing wrong

Upvotes: 0

Views: 339

Answers (1)

Ketan
Ketan

Reputation: 46

I guess you just forgot to insert "monthExpenseTest" in actual database, by means that you have just created a object by using below lines as you mentioned

Month_Expense_Application__c monthExpenseTest = new Month_Expense_Application__c(
            Balance__c = 12.3,
            Income__c = 11,
            Keeper__c = '0034x00001K7kGCAAZ',
            MonthDate__c = date.newInstance(2022, 11, 21)
        );

by writing above lines you just created object which is present in memory only. it is not stored in database, so that it is not fetched when you try to fetch it via SOQL Query. Now you may want to add one more line after above code.which is given as below

insert monthExpenseTest ;

so after inserting your object is stored in database. now you can fetch it via SOQL query.

So your test class will be looks like below

@isTest
public class ExpenseAdminControllerTests {
    @isTest
    public static void numberRecordsTests() {
        Month_Expense_Application__c monthExpenseTest = new Month_Expense_Application__c(
            Balance__c = 12.3,
            Income__c = 11,
            Keeper__c = '0034x00001K7kGCAAZ',
            MonthDate__c = date.newInstance(2022, 11, 21)
        );
        
        insert monthExpenseTest;
        Integer year = 2022;
        Integer keepers = ExpenseAdminController.numberRecords(year);
        System.assertEquals(1, keepers);
    }
}

So when you are testing ExpenseAdminController then you just need to call that method because you want to test it. in test class which is mentioned in question it not testing numberRecords() method instead of you are rewriting logic.

Upvotes: 3

Related Questions