+ Start a Discussion
bikla78bikla78 

Test method for trigger

All my test methods I have for events are failing is there a special way I need to write my test method since I am using the created date field for the business logic. It keeps saying 0% at least 1 line needs to be tested and i have several event test classess for my other triggers.Also all my other triggers call classes and this is the only one where the business logic is built inside the tirgger...do i have to create a new test method. if so, how do i call it from here

trigger Event_Before_Insert on Event (before update)
{
for( Event currentEvent : Trigger.new )
{
datetime CreatedDate =CurrentEvent.CreatedDate;
date ActivityDate =CurrentEvent.ActivityDate;

date createdDateTemp = date.newInstance(CreatedDate.year(),CreatedDate.month(),CreatedDate.day());
Integer numberofDays = createdDateTemp.daysBetween(ActivityDate);

CurrentEvent.Day_Count__c = numberofDays;


}

}

 

Message Edited by bikla78 on 07-31-2009 03:33 PM
snugglessnuggles
it looks like this is a before update trigger, are your other triggers insert triggers?  Do the test methods only insert records rather than doing an update to an existing record?  If you have test methods that do event updates, you should atleast get some coverage here.
bikla78bikla78

The other test methods are all insert tests but i have triggers that  is before insert and update.Below is one of my methods. How do I convert this into an update statement

 

 

Event ev48 = new Event(Subject = 'Test', Type= 'Other', Meeting_Status__c = 'Pending',ActivityDate =Date.today(),WhoId = cont.Id );

try {

insert event;

}

catch(DmlException e) {

System.debug(e.getMessage());

}

 

 

Message Edited by bikla78 on 07-31-2009 04:13 PM
snugglessnuggles

This test never tests the update trigger because you never do an update to the event.  You should take that event you just inserted and update it.  Below is a simple example.  Although ideally you would write the test method to actually use assertions to make sure your code is working properly ;)

 

Event ev48 = new Event(Subject = 'Test', Type= 'Other', Meeting_Status__c = 'Pending',ActivityDate =Date.today(),WhoId = cont.Id );

try {

insert event;

}

catch(DmlException e) {

System.debug(e.getMessage());

}

event.custom_field__c = "test";

update event;

 

bikla78bikla78

I tried this but it is still saying zero percent. 

 

Event ev48 = new Event(Subject = 'Test', Type= 'Other', Meeting_Status__c = 'Pending',ActivityDate =Date.today(),WhoId = cont.Id );

try {

insert ev48;

}

catch(DmlException e) {

System.debug(e.getMessage());

}

ev48.day_count__c =0;

update ev48;