Skip to main content
added 245 characters in body
Source Link
Pranay Jaiswal
  • 36.4k
  • 17
  • 79
  • 138

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__rcountryWebFormText__c,Country_of_Origin__r.name,Country_of_Origin__C 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.

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__r.name,Country_of_Origin__C from Case where id=:caseObj.id limit 1];

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

}

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.

Source Link
Pranay Jaiswal
  • 36.4k
  • 17
  • 79
  • 138

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__r.name,Country_of_Origin__C from Case where id=:caseObj.id limit 1];

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

}