You need to sign in to do that
Don't have an account?
jeveleth
Apex trigger and code coverage
I'm trying to create an Apex trigger that will prevent a record on the custom object Work Order from being deleted if a box in the object (Ready For Invoice) is checked. The trigger fires in my sandbox, but my unit tests seem to cover 0% of the trigger. I've walked through the relevant sections of the Force.com workbook on testing (Tutorials 7 and 8), but I'm having little luck. Can anyone advise?
The code is below:
//Trigger trigger noDeleteReadyForInvoice on Work_Order__c (before delete) { for (Work_Order__c q: trigger.old) if (q.Ready_for_Invoice__c == true) { q.AddError('Cannot delete Work Order that is ready for invoice.'); } } //Test @isTest private class noDeleteReadyForInvoice { static testMethod void testWorkOrderTrigger(){ //First, prepare 200 work orders for the test data Account a = new Account(Name='test account'); insert a; Projects__c p = new Projects__c(Account__c=a.Id,Project_Name__c='test project'); insert p; Work_Order__c[] wo = new Work_Order__c[]{ new Work_Order__c(Project__c=p.Id,Work_Order_Description__c='test description',Ready_for_Invoice__c=true) }; insert wo; //Now insert data causing a work order trigger to fire. Test.startTest(); update wo; Test.stopTest();
Update:
I've change "update wo" to "delete wo" in the test, but the trigger's code still doesn't seem to be covered: http://i.imgur.com/GGsb2.png.
Update. The test below works. Thanks for the assistance.
All Answers
Your trigger uses "before delete" only but your test is running an update, not a delete.
You need to run
Kevin,
Thanks, but the code still doesn't seem to be covered: http://i.imgur.com/GGsb2.png even after I change update to delete.
The message in the linked image points to the first line of code in the trigger. The message reads as follows:
"Multiple markers at this line:
- Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
- File only saved locally, not to server."
When I attempt to save the file to the server, I get a message that my trigger is "dirty." That said, even after I've saved the trigger to the server, I continue to get the red cross.
The coverage window at the bottom shows 100% coverage. I think you are good, you mught just have a dirty file. Are you editing in both the IDE and the Salesforce UI? That can cause issues.
Do this...
1) In the IDE, right-click the src folder and select "Refresh from Server"
2) Run your tests again.
Thanks, Kevin. Alas, I now get the following failure. Any notion what I've done wrong?
Hello,
Use this:
@isTest
private class noDeleteReadyForInvoice
{
static testMethod void testWorkOrderTrigger()
{
Account a = new Account(Name='test account');
insert a;
Projects__c p = new Projects__c(Account__c=a.Id,Project_Name__c='test project');
insert p;
Work_Order__c wo = new Work_Order__c(Project__c=p.Id,Work_Order_Description__c='test description',Ready_for_Invoice__c=true);
insert wo;
delete wo;
}
}
Think this will help you to cover.
Thanks, but I still get the following error.
Update. The test below works. Thanks for the assistance.