You need to sign in to do that
Don't have an account?
Niknit
This code is working, but I don't understand why are we using The LIMIT and WHERE Id =:newstudent.Id. if there is only one record I am inserting then why LIMIT and use where clause with ID.The test fails if I don't use this.
When do we use LIMIT and where clause with ID in SOQL query?
@testSetup static void testData() { Department__c newdept = new department__c(); newdept.name='CSE'; insert newdept; } @isTest static void validateupdatechange() { Department__c newDept = [SELECT Id, no_of_students__c FROM Department__c LIMIT 1]; student_details__c newstudent = new Student_details__c(); newstudent.Name='From TestClass'; newstudent.department__c = newDept.Id; System.debug('before@@trigger: ' + newDept.no_of_students__c); test.startTest(); insert newstudent; test.stopTest(); newstudent = [SELECT department__c FROM student_details__c WHERE Id =:newstudent.Id]; newDept = [SELECT no_of_students__c FROM Department__c LIMIT 1]; System.debug('after@@trigger: ' + newDept.no_of_students__c); System.assertEquals(1 , newDept.no_of_students__c); }
This code is working, but I don't understand why are we using The LIMIT and WHERE Id =:newstudent.Id. if there is only one record I am inserting then why LIMIT and use where clause with ID.The test fails if I don't use this.
You are right you added only one record then without limit also you can get record. but if your code contain SeeAllData=true in that case your code will fail as that will return list of record and you are storing same in single object. In that case you code should be like below
List<Department__c> newDept = [SELECT Id, no_of_students__c FROM Department__c ];
If you are using code like below then try to add limit 1 as you can story only one record.
Department__c newDept = [SELECT Id, no_of_students__c FROM Department__c LIMIT 1];
let us know if you need more detail