You need to sign in to do that
Don't have an account?

If my dml insert happeneds in a test and that information is not committed to the DB how can my assertions work?
I have this test code to test if an account is enabled.
@isTest private class PIContractsCronTest { @isTest static void PIContractsCronTest() { //Scenario: ///Given a nightly build at 11:59 //Arrange Account acct = new Account(); acct.Name = 'Test Account - Sheldon 3'; acct.recordTypeId = '01250000000DfwkAAC'; acct.Primary_Sector__c = 'General Inclusion'; acct.Name_Code__c = 'TSTSHEL'; insert acct; Account parentAcct = [SELECT Id,Name FROM Account WHERE Name = 'Test Account - Sheldon 3']; PI_Contract__c pic = new PI_Contract__c(Name ='Test Contract', Account__c = parentAcct.Id, Local_Currency_Peg_Enabled__c = 'No', Contract_Start_Date__c = DATE.TODAY()); insert pic; // When an Account object has a PI CONTRACT with the “Status = current” AND current date is greater than or equal to “Contract Start Date” //Act Test.StartTest(); PIContractsCron cron = new PIContractsCron(); Account checkPI = [SELECT Id,Name, PI_Enabled__c FROM Account WHERE Id = :parentAcct.Id]; Test.StopTest(); // Then set the field PI Contracts Enabled to true on the Account Object //Assert System.debug('Check PI:::'+checkPI); System.assertEquals(true,checkPI.PI_Enabled__c,'PI is not Enabled on this Account'); } }
This code runs this function
public class PIContractsCron { public PIContractsCron() { List<PI_Contract__c> contractList = [SELECT Id, Status__c, Contract_Start_Date__c, Account__c FROM PI_Contract__c WHERE Status__c = 'Current' AND Contract_Start_Date__c >= TODAY]; Map <Id, Account> acctMap = new Map<Id, Account>(); for(PI_Contract__c contract: contractList){ Id acctNo = contract.Account__c; Account acct = [SELECT Id, PI_Enabled__c FROM Account WHERE Id = :acctNo]; acctMap.put(acctNo,acct); } List <Account> enabledAcct = new List<Account>(); for(Account a: acctMap.values()){ a.PI_Enabled__c = true; enabledAcct.add(a); } System.debug('Enabled Accounts:::'+ enabledAcct); update enabledAcct; } }
I can tell that the dml from the test is not showing up in this class because the System.debug('Enabled Accounts:::'+ enabledAcct); comes back empty. What am I doing wrong?
The error is in your test class is have not filled the value of account field 'PI_Enabled__c ' during insertion thats the reason your assertion is not showing the proper result.
And always try to write assert statement inside Test.StartTest(); and Test.StopTest(); and one more thing, in your apex class you have written SOQL inside a for loop please try to written soql outside the for loop because it can hit the governor limit. I have highlighted the changes I have made in your test class with **.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha