3

I keep getting an assertion failure with my test class for the following trigger:

   trigger WebToCaseCountry on Case (after insert)
{

    Set<String> countries = new Set<String>();
    for (Case c : Trigger.new)
    {
        if (!String.isEmpty(c.CountryWebFormText__c) && c.Newsletter__c == TRUE)
        {
            countries.add(c.CountryWebFormText__c);
        }
    }

    if (countries.size() > 0)
    {
        Map<String, Id> filteredCountries = new Map<String, Id>();
        for (Area__c a : [select Name, Id from Area__c where Name in :countries])
        {
            filteredCountries.put(a.Name, a.Id);
        }
        if (filteredCountries.size() > 0)
        {
            List<Case> caseUpdates = new List <Case>();
            List<Contact> contactUpdates = new List <Contact>();
            for (Case c : Trigger.new)
            {
                if (filteredCountries.containsKey(c.CountryWebFormText__c))
                {
                    Id areaId = filteredCountries.get(c.CountryWebFormText__c);
                    caseUpdates.add(new Case(Id = c.Id, Country_of_Origin__c = areaId));
                    if (c.ContactId != null)
                    {
                        contactUpdates.add(new Contact(Id = c.ContactId, Country__c = areaId));
                    }
                }
            }
            update caseUpdates;
            update contactUpdates;
        }
    }
}

This is my test class:

@isTest(SeeAllData=true)
public class WebToCaseCountryTest
{

    public static testMethod void testTrigger()
    {
        Area__c ar = new Area__c(Name = 'CA');
    insert ar;

    Case caseObj = new Case(CountryWebFormText__c ='CA', Newsletter__c = TRUE);
    insert caseObj;
    System.debug(caseObj);

    System.assertEquals(caseObj.CountryWebFormText__c, caseObj.Country_of_Origin__c);

    }
}

This is the error I am getting:

System.AssertException: Assertion Failed: Expected: CA, Actual: null -- STACK TRACE: Class.WebToCaseCountryTest.testTrigger: line 19, column 1

Anyone know why this is happening?

highfive
  • 6,241
  • 3
  • 30
  • 56
Nik
  • 125
  • 2
  • 10

2 Answers2

1

In You logic you are checking for ContactID

if (c.ContactId != null)
{
    contactUpdates.add(new Contact(Id = c.ContactId, Country__c = areaId));
}

But in your test method you didn't even created a contact record. So how would above condition work without contact. It will always return false and nothing will be updated. That will cause assert failure.

Ashwani
  • 22,582
  • 4
  • 38
  • 72
  • Hi @Ashwani I am just in the process of creating a contact now, but I don't see how this would affect the Case.Country_of_Origin__c field? That should contain a value right? – Nik Aug 20 '15 at 08:41
1

Try this

public static testMethod void testTrigger()
{
    Area__c ar = new Area__c(Name = 'CA');
insert ar;

Case caseObj = new Case(CountryWebFormText__c ='CA', Newsletter__c = TRUE);
insert caseObj;
System.debug(caseObj);
caseObj=[Select id,countryWebFormText__c,Country_of_Origin__r.name from Case where id=:caseObj.id limit 1];

System.assertEquals(caseObj.CountryWebFormText__c, caseObj.Country_of_Origin__r.name);

}

From what we know is Country_of_Origin__C is a lookup field of type Area__C . caseObj.Country_of_Origin__c will only contain the ID of Area__C record, while what you want is the name of Area__C record. So to get the Name you have to query it as Country_of_Origin__R.name get the name of inserted Area.

As commented , seeAllData as true is generally not recommended.

Pranay Jaiswal
  • 35,996
  • 16
  • 75
  • 134
  • Thank you so much! I notice the Limit 1 and that actually was an error I was getting previously, that it was returning more than 1 and I wasn't sure why. But now it makes sense. This has been so helpful I am going to read up more on testing and triggers with multiple objects and I will definitely refer to this in future if I have any problems. Thanks again :) – Nik Aug 20 '15 at 09:14
  • 1
    Yeah.. keep good work flow.. Here is something for your reference on writing best test cases,,, http://salesforce.stackexchange.com/questions/8558/when-testing-can-i-not-use-existing-data – Pranay Jaiswal Aug 20 '15 at 10:03