You need to sign in to do that
Don't have an account?
Matthew A Small
My Test class has me stumped!
Hi everyone.
I am relatively new to writing test classes and I am creating a package.
I get this error when I try to upload into the package.
Here is my test class
@isTest
private class CheckInShiftCreateCtrlTest {
@isTest static void testConstructor_ParentShiftExist() {
Shift__c shift= new Shift__c();
insert shift;
shift= [select Id, Name from Shift__c where id = : shift.id];
Contact_Report__c contRepForInsert = new Contact_Report__c(
Shift__c = shift.Id
);
insert contRepForInsert;
Test.setCurrentPage(Page.CheckInCreate);
ApexPages.CurrentPage().getparameters().put('pid', contRepForInsert.Id);
Test.startTest();
ApexPages.StandardController sc = new ApexPages.standardController(contRepForInsert);
CheckInShiftCreateCtrl ctrl = new CheckInShiftCreateCtrl(sc);
Test.stopTest();
System.assertEquals(shift.Id, ctrl.contactRep.Shift__c);
}
@isTest static void testConstructor_NoParentShift() {
Contact_Report__c contRepForInsert = new Contact_Report__c();
insert contRepForInsert;
Test.setCurrentPage(Page.CheckInCreate);
ApexPages.CurrentPage().getparameters().put('pid', contRepForInsert.Id);
Test.startTest();
ApexPages.StandardController sc = new ApexPages.standardController(contRepForInsert);
CheckInShiftCreateCtrl ctrl = new CheckInShiftCreateCtrl(sc);
Test.stopTest();
System.assert(ctrl.contactRep != null);
}
@isTest static void testRefreshFilterResult() {
Shift__c shift2= new Shift__c();
insert shift2;
shift2= [select Id, Name from Shift__c where id = : shift2.id];
Shift__c contRepForInsert = new shift__c();
insert contRepForInsert;
contRepForInsert= [select Id, Name from Shift__c where id = : contRepForInsert.id];
Test.setCurrentPage(Page.CheckInCreate);
ApexPages.CurrentPage().getparameters().put('pid', contRepForInsert.Id);
Test.startTest();
ApexPages.StandardController sc = new ApexPages.standardController(contRepForInsert);
CheckInShiftCreateCtrl ctrl = new CheckInShiftCreateCtrl(sc);
ctrl.selectedShiftName = 'test';
ctrl.refreshFilterResults();
Integer countRecords1 = ctrl.allFilteredShift.size();
ctrl.selectedShiftName = '';
ctrl.refreshFilterResults();
Integer countRecords2 = ctrl.allFilteredShift.size();
ctrl.selectedShiftName = 'testShift2';
ctrl.refreshFilterResults();
Integer countRecords3 = ctrl.allFilteredShift.size();
Test.stopTest();
System.assertEquals(3, countRecords1);
System.assertEquals(3, countRecords2);
System.assertEquals(2, countRecords3);
}
@isTest static void testSelectShift() {
Shift__c shift = new Shift__c();
insert shift ;
shift= [select Id, Name from Shift__c where id = : shift.id];
Shift__c shift2 = new Shift__c();
shift2= [select Id, Name from Shift__c where id = : shift2.id];
insert new List<Shift__c>{shift, shift2};
Shift__c contRepForInsert = new Shift__c();
insert contRepForInsert ;
contRepForInsert = [select Id, Name from Shift__c where id = : contRepForInsert .id];
Test.setCurrentPage(Page.ContactReportCreate);
ApexPages.CurrentPage().getparameters().put('pid', contRepForInsert.Id);
Test.startTest();
ApexPages.StandardController sc = new ApexPages.standardController(contRepForInsert);
CheckInCreateCtrl ctrl = new CheckInCreateCtrl(sc);
ctrl.refreshFilterResults();
ApexPages.CurrentPage().getparameters().put('shiftName', 'testShift2');
Test.stopTest();
System.assertEquals(shift.Id, ctrl.contactRep.Shift__c);
}
}
Can anyone help?
I am relatively new to writing test classes and I am creating a package.
I get this error when I try to upload into the package.
Here is my test class
@isTest
private class CheckInShiftCreateCtrlTest {
@isTest static void testConstructor_ParentShiftExist() {
Shift__c shift= new Shift__c();
insert shift;
shift= [select Id, Name from Shift__c where id = : shift.id];
Contact_Report__c contRepForInsert = new Contact_Report__c(
Shift__c = shift.Id
);
insert contRepForInsert;
Test.setCurrentPage(Page.CheckInCreate);
ApexPages.CurrentPage().getparameters().put('pid', contRepForInsert.Id);
Test.startTest();
ApexPages.StandardController sc = new ApexPages.standardController(contRepForInsert);
CheckInShiftCreateCtrl ctrl = new CheckInShiftCreateCtrl(sc);
Test.stopTest();
System.assertEquals(shift.Id, ctrl.contactRep.Shift__c);
}
@isTest static void testConstructor_NoParentShift() {
Contact_Report__c contRepForInsert = new Contact_Report__c();
insert contRepForInsert;
Test.setCurrentPage(Page.CheckInCreate);
ApexPages.CurrentPage().getparameters().put('pid', contRepForInsert.Id);
Test.startTest();
ApexPages.StandardController sc = new ApexPages.standardController(contRepForInsert);
CheckInShiftCreateCtrl ctrl = new CheckInShiftCreateCtrl(sc);
Test.stopTest();
System.assert(ctrl.contactRep != null);
}
@isTest static void testRefreshFilterResult() {
Shift__c shift2= new Shift__c();
insert shift2;
shift2= [select Id, Name from Shift__c where id = : shift2.id];
Shift__c contRepForInsert = new shift__c();
insert contRepForInsert;
contRepForInsert= [select Id, Name from Shift__c where id = : contRepForInsert.id];
Test.setCurrentPage(Page.CheckInCreate);
ApexPages.CurrentPage().getparameters().put('pid', contRepForInsert.Id);
Test.startTest();
ApexPages.StandardController sc = new ApexPages.standardController(contRepForInsert);
CheckInShiftCreateCtrl ctrl = new CheckInShiftCreateCtrl(sc);
ctrl.selectedShiftName = 'test';
ctrl.refreshFilterResults();
Integer countRecords1 = ctrl.allFilteredShift.size();
ctrl.selectedShiftName = '';
ctrl.refreshFilterResults();
Integer countRecords2 = ctrl.allFilteredShift.size();
ctrl.selectedShiftName = 'testShift2';
ctrl.refreshFilterResults();
Integer countRecords3 = ctrl.allFilteredShift.size();
Test.stopTest();
System.assertEquals(3, countRecords1);
System.assertEquals(3, countRecords2);
System.assertEquals(2, countRecords3);
}
@isTest static void testSelectShift() {
Shift__c shift = new Shift__c();
insert shift ;
shift= [select Id, Name from Shift__c where id = : shift.id];
Shift__c shift2 = new Shift__c();
shift2= [select Id, Name from Shift__c where id = : shift2.id];
insert new List<Shift__c>{shift, shift2};
Shift__c contRepForInsert = new Shift__c();
insert contRepForInsert ;
contRepForInsert = [select Id, Name from Shift__c where id = : contRepForInsert .id];
Test.setCurrentPage(Page.ContactReportCreate);
ApexPages.CurrentPage().getparameters().put('pid', contRepForInsert.Id);
Test.startTest();
ApexPages.StandardController sc = new ApexPages.standardController(contRepForInsert);
CheckInCreateCtrl ctrl = new CheckInCreateCtrl(sc);
ctrl.refreshFilterResults();
ApexPages.CurrentPage().getparameters().put('shiftName', 'testShift2');
Test.stopTest();
System.assertEquals(shift.Id, ctrl.contactRep.Shift__c);
}
}
Can anyone help?
CheckInShiftCreateCtrlTest.testRefreshFilterResultSystem.TypeException: Invalid conversion from runtime type Shift__c to Contact_Report__cClass.CheckInShiftCreateCtrl.<init>: line 12, column 1 Class.CheckInShiftCreateCtrlTest.testRefreshFilterResult: line 59, column 1
CheckInShiftCreateCtrlTest.testSelectShiftSystem.QueryException: List has no rows for assignment to SObjectClass.CheckInShiftCreateCtrlTest.testSelectShift: line 84, column 1
One thing I notice is that everywhere in your test code you are attempting to assign the result of your Soql Query to a single Sobject instance instead of a List of Sobjects. The error List has no rows for assignment to Sobject occurs when you attempt to assign the result of your Soql Query to a single Sobject instance and the query result does not return an records matching the given criteria.
Update the code within the testSelectShift() method as follows as well as the other test methods. Please mark the thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.
Error: Compile Error: Invalid type: Int at line 84 column 9
for(Int shiftCnt = 0; shiftCnt < 10; shiftCnt++){ is the line in question.
Do you know what went wrong here?
Thanks again,
Matt
The "integer fix looked like it did the trick but I have another compile error now.
Error: Compile Error: Illegal assignment from Database.SaveResult to List<Database.SaveResult> at line 101 column 33
List<Database.SaveResult> contRepSaveResultList = Database.insert(contRepForInsert);
=(
CheckInShiftCreateCtrlTest.testRefreshFilterResultSystem.TypeException: Invalid conversion from runtime type Shift__c to Contact_Report__cClass.CheckInShiftCreateCtrl.<init>: line 12, column 1 Class.CheckInShiftCreateCtrlTest.testRefreshFilterResult: line 59, column 1
CheckInShiftCreateCtrlTest.testSelectShiftSystem.TypeException: Invalid conversion from runtime type Shift__c to Contact_Report__cClass.CheckInCreateCtrl.<init>: line 12, column 1 Class.CheckInShiftCreateCtrlTest.testSelectShift: line 110, column 1
Please share the complete code snippet that you have. Also, please use the "< > icon" in the answer editor to paste the code snippet.