You need to sign in to do that
Don't have an account?
Unit Test Example
trigger Sharing_on_Additional_Contact_Owner on Additional_Contact_Owner__c (before delete, after insert, after update)
{
if (Trigger.isInsert) //we must be inserting
{
Classes_and_Unit_Tests.InsertContactShare(Trigger.new[0].Contact_Name__c, Trigger.new[0].Additional_Contact_Owner_Name__c);
}
}
public class Classes_and_Unit_Tests
{
public static void InsertContactShare(id newContactId, id newOwnerId)
{
// given an Additional_Contact_Owner__c record, insert a new contactshare record providing Contact access to a User
try
{
//instantiate the object
ContactShare theShare = new ContactShare();
//update the object
theShare.ContactAccessLevel = 'Edit';
theShare.ContactId = newContactId;
theShare.UserOrGroupId = newOwnerId;
//insert it into salesforce
insert (theShare);
}
catch (Exception e)
{
//do nothing for now
}
} //end insertcontactshare method
} //end class
But you could add something like this into the class
static testMethod void TestSharing_on_Additional_Contact_Owner() {
Contact c = new Contact (
LastName = 'Test Contact');
insert c;
String aUserName = 'myname@mydomain.com%';
// the % wild card will enable you to use this across instances, assuming you are in a sandbox - if you in a dev environment, you would need to create a user for this part
User user = [select id from User where UserName like : aUserName LIMIT 1];
Additional_Contact_Owner__c aco = new Additional_Contact_Owner__c (
Contact_Name__c = c.id,
Additional_Contact_Owner_Name__c = user.id);
insert aco;
// you should then do some system asserts to check the record was created by selecting for the aco record that was created, and that the trigger created new record
}
Message Edited by JonSimmons on 11-07-2008 05:20 PM
So I suspect it is an issue with the test failing when it is being deployed - what I might suggest is creating the trigger and class directly in a different dev/clean sandbox environment, run the tests and see if it fails - if so, you should get an idea where the problem is.
Yeah, frustrating is a word for it, it would be super nice if salesforce actually provided some usefull error messages once in a while.
You are correct about the zero percent being because the code failed to save, the problem is figuring out why it failed to save since Salesforce isn't providing any usefull error messages.
What is frustrating me at the moment is that the unit test code (while testing from within eclipse) appears to prepare to call the trigger but doesn't actually call any of the trigger code as none of my system.debug messages show up in the log. It does appear to call code that the trigger should be calling but that code is failing for no apparent reason.
I continue to plod along
Thanks
Jon
20081108040731.026:Class.HarvestClassesandUnitTests.testDeleteContactShare: line 218, column 9: Insert: SOBJECT:Additional_Contact_Owner__c
*** Beginning Sharing_on_Additional_Contact_Owner on Additional_Contact_Owner trigger event AfterInsert for a0960000004JsDU
Cumulative resource usage:
Number of SOQL queries: 0 out of 100
Number of query rows: 0 out of 500
Number of SOSL queries: 0 out of 20
Number of DML statements: 3 out of 100
Number of DML rows: 3 out of 500
Number of script statements: 14 out of 200000
Maximum heap size: 0 out of 500000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 10
Number of System.runAs() invocations: 0 out of 20
20081108040731.026:Class.HarvestClassesandUnitTests.testDeleteContactShare: line 219, column 1: After the AfterInsert Trigger