You need to sign in to do that
Don't have an account?
Code question
I am getting the following error when executing my test code...does anyone know how to fix?:
System.AssertException: Assertion Failed: Expected: 1, Actual: null
Class.TestActivityCount.testCountTask: line 18, column 1
test code:
@istest
private class TestActivityCount{
/*
* Test method for this class and TaskUpdateContact and EventUpdateContact
*/
public static testMethod void testCountTask() {
//Setup
Contact con = new Contact(lastname='Test Con');
insert con;
//Insert our first task
Task t = new Task(subject='Test Activity', whoId = con.id, activity_Type__c='Visit', macro_service_line__c='ASC');
insert t;
//Verify count
con = [SELECT ID, activity_count__c FROM Contact WHERE ID = :con.id];
System.assertEquals(1,con.activity_count__c);
//Disconnect task from the contact
// didRun = false; //Reset
t.whatId = null;
update t;
//Verify count = 0
con = [SELECT ID, activity_count__c FROM Contact WHERE ID = :con.id];
System.assertEquals(0,con.activity_count__c);
//didRun = false; //Reset
//Add an event
Event e = new Event(subject='Test Event', whatId = con.id, startDateTime = System.Now(), endDateTime = System.now());
insert e;
//Verify count = 1
con = [SELECT ID, activity_count__c FROM Contact WHERE ID = :con.id];
System.assertEquals(1,con.activity_count__c);
//Relink the task to the contact
//didRun = false; //Reset
t.whatId = con.id;
update t;
//Verify count = 2
con = [SELECT ID, activity_count__c FROM Contact WHERE ID = :con.id];
System.assertEquals(2,con.activity_count__c);
//Disconnect the event from the contact
//didRun = false; //Reset
e.whatId = null;
update e;
//Verify count is back down to 1
con = [SELECT ID, activity_count__c FROM Contact WHERE ID = :con.id];
System.assertEquals(1,con.activity_count__c);
//Delete the task
// didRun = false; //reset
delete t;
//Verify count is back down to 0
con = [SELECT ID, activity_count__c FROM Contact WHERE ID = :con.id];
System.assertEquals(0,con.activity_count__c);
}
}
All Answers
Does it work when doing it through the UI?
Have you tried doing i through the UI with the exact parameters you do in your test class?
Test classes do not see your org's data. Is there something in your trigger that depends on some data you are not creating in this test class?
My Trigger may be the problem (sorry...new to this):
trigger TaskUpdateAccount on Task (after delete, after insert, after undelete, after update) {
Set<ID> conIds = new Set<ID>();
//We only care about tasks linked to contacts.
String prefix = ContactActivityCount.conPrefix;
//Add any contact ids coming from the new data
if (Trigger.new != null) {
for (Task t : Trigger.new) {
if (t.WhatId != null && String.valueOf(t.whatId).startsWith(prefix) ) {
conIds.add(t.whatId);
}
}
}
//Also add any contact ids coming from the old data (deletes, moving an activity from one contact to another)
if (Trigger.old != null) {
for (Task t : Trigger.old) {
if (t.WhatId != null && String.valueOf(t.whatId).startsWith(prefix) ) {
conIds.add(t.whatId);
}
}
}
if (conIds.size() > 0)
contactActivityCount.updatecontactCounts(conIds);
}
Thank you! After that update, the following error message appeared atwhen saving the trigger: Error: Compile Error: Illegal variable declaration: conIds.add at line 11 column 18
I must have had something wrong, because I was able to save now without errors!! Thank you for your help. One last question: Now my test class is saying: Compile Error: Variable does not exist: didRun at line 21 column 9
Updated test class:
@istest
private class TestActivityCount{
/*
* Test method for this class and TaskUpdateContact and EventUpdateContact
*/
public static testMethod void testCountTask() {
//Setup
Contact con = new Contact(lastname='Test Con');
insert con;
//Insert our first task
Task t = new Task(subject='Test Activity', whoId = con.id, activity_Type__c='Visit', macro_service_line__c='ASC');
insert t;
//Verify count
con = [SELECT ID, activity_count__c FROM Contact WHERE ID = :con.id];
System.assertEquals(1,con.activity_count__c);
//Disconnect task from the contact
didRun = false; //Reset
t.whatId = null;
update t;
//Verify count = 0
con = [SELECT ID, activity_count__c FROM Contact WHERE ID = :con.id];
System.assertEquals(0,con.activity_count__c);
didRun = false; //Reset
//Add an event
Event e = new Event(subject='Test Event', whatId = con.id, startDateTime = System.Now(), endDateTime = System.now());
insert e;
//Verify count = 1
con = [SELECT ID, activity_count__c FROM Contact WHERE ID = :con.id];
System.assertEquals(1,con.activity_count__c);
//Relink the task to the contact
didRun = false; //Reset
t.whatId = con.id;
update t;
//Verify count = 2
con = [SELECT ID, activity_count__c FROM Contact WHERE ID = :con.id];
System.assertEquals(2,con.activity_count__c);
//Disconnect the event from the contact
didRun = false; //Reset
e.whatId = null;
update e;
//Verify count is back down to 1
con = [SELECT ID, activity_count__c FROM Contact WHERE ID = :con.id];
System.assertEquals(1,con.activity_count__c);
//Delete the task
didRun = false; //reset
delete t;
//Verify count is back down to 0
con = [SELECT ID, activity_count__c FROM Contact WHERE ID = :con.id];
System.assertEquals(0,con.activity_count__c);
}
}
All is working perfectly now! Thank you so much for your help!