function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
deep12usdeep12us 

Need help on trigger

Hi, I was writing testcases for a trigger which keeps user and contact in sync, if users exists.

I'm creating a new contact , user and account  and inserting each of these objects But it gives me the error

assertion failed for email as I'm using system.asserteauals(contact.email, user.email)

Below is the code

 

@isTest
private class PL_ContactUserSyncControllerTest 
{
 static testMethod void ControllerShouldNotBeNULL() 
 {
 PL_ContactUserSyncController controller = new PL_ContactUserSyncController();
 system.assertNotEquals(controller, NULL);
    }
    static testMethod void ControllerContactUserSyncForOneUser()
 {

Contact c1 = new Contact();
c1 = insertContact();
User u1 = insertUser();
Contact c2 = [Select Email,Id from Contact where Id=:c1.Id];
c2.Email = 'me@catch.com';
update c2;
Contact c1u = [Select Email from Contact where Id=:c2.Id];
User u1u = [Select Email from User where Id=:u1.Id];
system.assertEquals(c1u.Email, u1u.Email);
system.debug(u1u.Email+ ' '+c1u.Email);
    }
    static Contact insertContact() 
{
Contact c1 = new Contact();
c1.AccountId = '001T000000Vk4ZzIAJ';
c1.FirstName = 'catch';
c1.LastName = 'me';
c1.Email = 'catch@me.com';
c1.Preferred_Language__c = 'English';
insert c1;
return c1;
}
static User insertUser()
{
Contact c = insertContact();
Profile p1 = [Select Id from profile where Id='00e60000000idGFAAY' limit 1];
UserRole r1 = [Select Id from UserRole where Id='00ET0000000rtysMAA' limit 1];
User u1 = new User();
u1.FirstName = c.FirstName;
u1.LastName = c.LastName;
u1.Username = 'catch@me.com';
u1.Language__c = 'English';
u1.ContactId = c.Id;
u1.Email = 'catch1@me.com';
u1.Alias = 'meu';
u1.TimeZoneSidKey = 'America/Los_Angeles';
u1.LocaleSidKey = 'en_US';
u1.EmailEncodingKey = 'ISO-8859-1';
u1.ProfileId = p1.Id;
u1.LanguageLocaleKey = 'en_US';
u1.UserRoleId = r1.Id;
u1.PortalRole = 'Worker';
insert u1;
return u1;
}
}

 

sdetweilsdetweil

well, the emails.. DON'T match

 

insertContact

c1.Email = 'catch@me.com';

 

insertuser()

u1.Email = 'catch1@me.com';

 

then u changed the contact

Contact c2 = [Select Email,Id from Contact where Id=:c1.Id];
c2.Email = 'me@catch.com';
update c2;
and they STILL aren't equal
Sam

 

deep12usdeep12us

There is a after update trigger, which updates user, when a contact email is updated even when they are not equal

sdetweilsdetweil

sorry, I don't understand..

 

you said 'But it gives me the error assertion failed for email '

 

yet the code you showed will definately cause the assertion message.

 

it will assert everytime with the code shown

u1u.email = 'catch1.me.com'

and

c2.Email = 'me@catch.com';

then you assertequals(c2.email, u1u.email); and they are not.

 

System.AssertEquals and System.AssertNotEquals both accept three parameters; the first two (mandatory) are the variables that will be tested for in/equality and the third (optional) is the message to display if the assert results in false

 

I think you wanted the other 'assert', AssertNOTEquals() to alert you if they are EQUAL (not equal = false means equal=true)

Sam