0

I wrote a trigger that works, and wrote a test class, but for some reason in the developer console in the code coverage list my trigger doesn't show up. When I tried to deploy the trigger to production, it wouldn't go through because it needs at least 1% coverage. Can someone tell me what I'm doing wrong with my test class or why my trigger isn't showing up for possible code coverage? Thanks!

Trigger

trigger RemoveContactSubscribers on Contact (after delete) {

    List<Contact_Tag__C> CTags = New List<Contact_Tag__C> ([Select c.id, c.contact2__c from Contact_Tag__C c where Contact2__c = :Null]);

    If(CTags.size()>0){
        try{
            Database.DeleteResult[] delete_Subscribers = Database.delete(CTags, false);

            for(Database.DeleteResult dr:delete_Subscribers){
                if(!dr.isSuccess())System.debug('@@@@@@@ '+dr.getErrors()[0]);
            }

        }catch (System.DmlException e) {
            for (Integer i = 0; i < e.getNumDml(); i++) {
                System.debug('@@@@@@@@@@ Error: '+e.getDmlMessage(i)); 
            }       
        }
    }
}

Test Class

@isTest
public class TestRemoveContactSubscribers {

    static testMethod void removeContactSubscribers(){

        Contact c = new Contact(LastName = 'Fred');

        Test.startTest();
        insert c; 
        System.assertEquals(c, true);
        Test.stopTest();

        Contact_Tag__c ct = new Contact_Tag__c(contact2__c = null, user__c = c.ownerId);

        Test.startTest();
        insert ct;
        Database.DeleteResult result = Database.delete(ct, false);
        Test.stopTest();

        System.assert(!result.isSuccess());

    }
}
3
  • you just created an instance of contact but never inserted the contact, so you do not have a contact to create you contact_tag__c. Later you never created a contact_tag__c you only delete it. As a good practice assert if contact was created, contact tag was created etc..
    – Rao
    Apr 16 '15 at 18:48
  • Thanks, I just made edits. Is this more of what you were saying?
    – Stephen
    Apr 16 '15 at 18:56
  • ... you don't need the Test.startTest(); for the insert c; lines because that's not what you are actually testing... Apr 16 '15 at 20:02
1

You'll need to have a few more steps here... You weren't tying your ct to the c you created, you weren't deleting c (which is what you were trying to test), and you also weren't asserting that ct was successfully deleted after you deleted c

@isTest
public class TestRemoveContactSubscribers {

    static testMethod void removeContactSubscribers(){

        Contact c = new Contact(LastName = 'Fred');
        insert c; 
        System.assertNotEquals(null, c.Id); // c will only have an Id if insertion was successful

        Contact c2 = [SELECT Id, OwnerId FROM Contact WHERE Id = :c.Id]; 

        Contact_Tag__c ct = new Contact_Tag__c(contact2__c = c2.Id /* you need to tie this to your newly created contact */, user__c = c2.ownerId /* insertion doesn't return the ownerid back to your c object, so we re-query this into c2 */);
        insert ct;

        System.assert(![SELECT Id FROM Contact_Tag__c WHERE Contact2__c = :c.Id].isEmpty()); // make sure there is at least one contact tag on your contact c

        Test.startTest();
        Database.DeleteResult result = Database.delete(c, false); // you want to test deleting c, not ct
        Test.stopTest();

        System.assert([SELECT Id FROM Contact_Tag__c WHERE Contact2__c = :c.Id].isEmpty()); // make sure there are no contact tags remaining

        System.assert(!result.isSuccess());

    }
}
2
  • Thanks for your help Nathan! For some weird reason it won't let me test if this works because my trigger isn't showing up in the developer console 'Overall Code Coverage' list. Do you have any idea why that might be?
    – Stephen
    Apr 16 '15 at 20:59
  • Likely because you haven't run a test class that actually tests it yet (salesforce has some weird processing on the backend for that. Once you run this particular test that actually deletes a contact, it should show up in dev console... Apr 16 '15 at 21:27
0

I modified your test class try with this code.

public class TestRemoveContactSubscribers {

static testMethod void removeContactSubscribers(){

    Contact c = new Contact(LastName = 'Fred');

    insert c; 

    Contact_Tag__c ct = new Contact_Tag__c(contact2__c = null, user__c = c.ownerId);
    insert ct;

    Test.startTest();
    Database.DeleteResult result = Database.delete(ct, false);
    Test.stopTest();

    System.assert(!result.isSuccess());

}

}

2
  • still deleting "ct" instead of "c" Apr 16 '15 at 20:08
  • ... and contact2__c is null and not set to c.Id Apr 16 '15 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.