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

Help With Test Class Please
Hi
I have written a trigger on sandbox. But when I am writing a test class I am getting 0% coverage.
I am posting both my trigger and test class here.
Can someone please check and tell me where I have gone wrong?
trigger CreateNewShowing on pb__Showing__c (after update) {
integer i=1;
for (pb__Showing__c sh : trigger.new) {
pb__Showing__c s = new pb__Showing__c();
datetime newDate = sh.Provisional_Date_A__c;
if(sh.Status__c == 'Pending' && sh.Provisional_Date_A__c != Null){
s.Account__c = sh.Account__c;
s.pb__Inventory__c = sh.pb__Inventory__c;
s.Start_Time__c = newDate ;
s.End_Time__c = newDate.addHours(2);
insert s;
}
pb__Showing__c s1 = new pb__Showing__c();
datetime newDate1 = sh.Provisional_Date_B__c;
if(sh.Status__c == 'Pending' && sh.Provisional_Date_B__c != Null){
s1.Account__c = sh.Account__c;
s1.pb__Inventory__c = sh.pb__Inventory__c;
s1.Start_Time__c = newDate1 ;
s1.End_Time__c = newDate1.addHours(2);
insert s1;
}
pb__Showing__c s2 = new pb__Showing__c();
datetime newDate2 = sh.Provisional_Date_C__c;
if(sh.Status__c == 'Pending' && sh.Provisional_Date_C__c != Null){
s2.Account__c = sh.Account__c;
s2.pb__Inventory__c = sh.pb__Inventory__c;
s2.Start_Time__c = newDate2 ;
s2.End_Time__c = newDate2.addHours(2);
insert s2;
}
pb__Showing__c s3 = new pb__Showing__c();
datetime newDate3 = sh.Provisional_Date_D__c;
if(sh.Status__c == 'Pending' && sh.Provisional_Date_D__c != Null){
s3.Account__c = sh.Account__c;
s3.pb__Inventory__c = sh.pb__Inventory__c;
s3.Start_Time__c = newDate3 ;
s3.End_Time__c = newDate3.addHours(2);
insert s3;
}
pb__Showing__c s4 = new pb__Showing__c();
datetime newDate4 = sh.Provisional_Date_E__c;
if((sh.Status__c == 'Pending')&&(sh.Provisional_Date_E__c != Null)){
s4.Account__c = sh.Account__c;
s4.pb__Inventory__c = sh.pb__Inventory__c;
s4.Start_Time__c = newDate4 ;
s4.End_Time__c = newDate4.addHours(2);
insert s4;
}
pb__Showing__c s5 = new pb__Showing__c();
datetime newDate5 = sh.Provisional_Date_F__c;
if(sh.Status__c == 'Pending' && sh.Provisional_Date_F__c != Null){
s5.Account__c = sh.Account__c;
s5.pb__Inventory__c = sh.pb__Inventory__c;
s5.Start_Time__c = newDate5 ;
s5.End_Time__c = newDate5.addHours(2);
insert s5;
}
}
}
Test Class
@isTest
private class UnitTestCreateNewShowing {
static testMethod void myUnitTest() {
// TO DO: implement unit test
User u = [select id from user where LastName = 'Walsh'];
//create test account
Account a = new Account();
a.LastName = 'testabcdef';
a.PersonEmail='test@testsnk.com';
a.Phone='987654';
a.OwnerId = u.id;
a.service_breach__c = false;
a.pb__Status__c = 'Unqualified';
insert a;
pb__InventoryItem__c inv = new pb__InventoryItem__c();
inv.name = 'abc123';
inv.pb__IsForSale__c = true;
inv.pb__PurchaseListPrice__c = 12313;
inv.pb__IsAvailable__c = true;
inv.Project__c = 'abcdd';
inv.pb__ItemStatus__c = 'Available';
inv.pb__IsBlocked__c = false;
inv.SellerId__c = a.id;
inv.Area__c = 'Marina';
inv.Unit_No__c = '12test';
inv.ItemStage__c = 'Available';
insert inv;
inv.OwnerId = u.id;
update inv;
//B
pb__InventoryItem__c inv1 = new pb__InventoryItem__c();
inv1.name = 'abc123';
inv1.pb__IsForSale__c = true;
inv1.pb__PurchaseListPrice__c = 12313;
inv1.pb__IsAvailable__c = true;
inv1.Project__c = 'abcdd';
inv1.pb__ItemStatus__c = 'Available';
inv1.pb__IsBlocked__c = false;
inv1.SellerId__c = a.id;
inv1.Area__c = 'Marina';
inv1.Unit_No__c = '12test';
inv1.ItemStage__c = 'Available';
insert inv1;
inv1.OwnerId = u.id;
update inv1;
pb__InventoryItem__c inv2 = new pb__InventoryItem__c();
inv2.name = 'abc123';
inv2.pb__IsForSale__c = true;
inv2.pb__PurchaseListPrice__c = 12313;
inv2.pb__IsAvailable__c = true;
inv2.Project__c = 'abcdd';
inv2.pb__ItemStatus__c = 'Available';
inv2.pb__IsBlocked__c = false;
inv2.SellerId__c = a.id;
inv2.Area__c = 'Marina';
inv2.Unit_No__c = '12test';
inv2.ItemStage__c = 'Available';
insert inv2;
inv2.OwnerId = u.id;
update inv2;
Datetime todays= datetime.now();
pb__Showing__c s = new pb__Showing__c();
s.Start_Time__c=todays;
s.End_Time__c=todays.addHours(2);
s.Account__c=a.id;
s.pb__Inventory__c=inv2.id;
s.Provisional_Date_A__c = todays.addDays(2);
s.Status__c = 'Pending';
insert s;
}
}
Kind Regards
Finney
Hi Finney,
as per test class you have craeted the record for pb__Showing__c object .Please have a update on this.which will work for you.
like
s.Status__c = 'Working';
update s;
Thanks,
Itswas
All Answers
Hi Finney,
as per test class you have craeted the record for pb__Showing__c object .Please have a update on this.which will work for you.
like
s.Status__c = 'Working';
update s;
Thanks,
Itswas
Thanks for your suggestion mate but 'Pending' is the default status for every showing record.
We don't have a Working value in the Status picklist. So I think it doesn't make any difference.
Kind Regards
Finney
Agreed with Itswas,
Your trigger is running on update event you need to update pb__Showing__c record to invoke it like :-
s.Status__c = 'Working';
update s;
I didn't understand the logic first.
Thanks for your explanation Vinit.