0

I am writing a test class for a trigger and am having some difficulty developing a test method.

My trigger is on the CampaignMember object. When a new member is inserted AND that CampaignMember is a contact AND a checkbox called "Provider_Sales_Campaign__c on the associated campaign is true, a new lead is created and tied to the same campaign (with a new campaign member). For my test class, I have created a contact and associated with with a campaign member (and in turn associated that with a PRovider_Sales_Campaign__c. Below is what I have of the test class so-far and I have indicated where I am missing test code.

@isTest 
public class LeadGenfromCampaignMember_Test {
    static testMethod void TestCampaignMember (){

        Test.startTest();

        //Creates Contact to be linked to Campaign Member
        Contact testContact = new Contact(FirstName = 'TestContactF', LastName = 'TestContactL', Email = '[email protected]');
        insert testContact;

        //Creates a new campaign memeber, associaites it with 1 provider sales campaign, and inserts
        CampaignMember newMember = 
        new CampaignMember(Campaign = [SELECT Id FROM Campaign WHERE Provider_Sales_Campaign__c = True LIMIT 1], Contact = testContact);
        insert newMember;

     //MISSING SYSTEM.ASSERT() CODE HERE!!!!!   

        Test.stopTest();

    }
}

Thanks in advance for any input.

3
  • So, we are expected to write your asserts for you, not knowing what your trigger is doing, not knowing your business requirements, etc??? That is like asking someone to audit your books only giving the final total for them to look at. You are the best person to write the asserts, start by gathering the results and asserting that they are as expected. Query for records, then system.assertEquals(xxx,xxx); If you have a more specific question, please add it to the post
    – Eric
    Aug 25, 2015 at 16:22
  • It needs to verify that a lead WAS created and that it was associated with the campaign. One avenue I have tried is querying the lead table to see if one has been created with that associated campaign ID. @Eric no need for you to write me anything, input is appreciated thanks.
    – Chris P
    Aug 25, 2015 at 16:26
  • More specifically, due to the order of operations, If I query the lead table to see if any new leads were created today by my trigger(and are Provider_Sales_Campaign__c's), will they even be indexed yet to query?
    – Chris P
    Aug 25, 2015 at 16:30

2 Answers 2

1

Based on your comment this should work but is not all inclusive

Lead[] l = [Select ID, CampaignID From Lead];

system.assertEquals(1,l.size(),'Either no lead was created or multiple leads were created');
system.assertEquals(YOURCAMPAIGNID,l[0].CampaignID,'The Lead was not associated with the proper campaign');

This assumes you are NOT setting seealldata=true

If you are setting seealldata=true, stop, rethink your test class to create the appropriate records first.

Since I do not see a link in your test class to a lead or a link from the new lead to an object in your test class, all you have to go on for assertions in the data size being one. If you can filter the leads in the test by a known link that will help narrow the query..

To answer you last comment:

the records will be available UNLESS they are created in an async method. If the are then you need to wrap the code that fires of the code you are testing in a test.startTest() and test.stoptest(). After the stoptest you can do the query as stoptest forces the async to process

3
  • Thanks this was helpful in putting me on the right path.
    – Chris P
    Aug 25, 2015 at 16:32
  • @ChrisP - Cool. If you have another question related to this directly, please add a comment and I will update the answer. If it solved your question, please mark as accepted so the community will know it is answered. Also, read up on how to ask a question, the more detailed and specific you are the better chance of getting amazing answers. This is a smart group here and if you spend the time asking the question in detail, you will be amazed at the answer you get.
    – Eric
    Aug 25, 2015 at 16:35
  • @ChrisP - Also, pay close attention to what sfcfox added, he has made some excellent comments regarding other issues with your test class....
    – Eric
    Aug 25, 2015 at 16:39
3

Your unit test has several problems, besides just needing some asserts. First, you assigned a value to Campaign instead of CampaignId on CampaignMember, and Contact instead of ContactId:

CampaignMember newMember = 
    new CampaignMember(CampaignId = [SELECT Id FROM Campaign WHERE Provider_Sales_Campaign__c = True LIMIT 1], ContactId = testContact.Id);

Secondly, you are not using "SeeAllData", but you're trying to query a campaign. So, you need to create a campaign first:

Campaign c = new Campaign(Name='Test', Provider_Sales_Campaign__c=true);
insert c;

... newMember = new CampaignMember(CampaignId=c.Id ...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .