+ Start a Discussion
BaguiarBaguiar 

Help with a simple test method on Event

Stuck with a test method for this trigger and I get the error:

 

Failure Message: "System.AssertException: Assertion Failed: Expected: Open, Actual: O​pen", Failure Stack Trace: "Class.testEventTrigger.test1: line 10, column 5 External entry point"

 

 

Here is the test method:

 

public class testEventTrigger {
  public static testMethod void test1() {
    List<Lead> leads = new List<Lead>{ new Lead(FirstName='Test 1',LastName='Test 1',Status='Open',Company='Test1',Sales_Cycle__c='O​pen'),
                                       new Lead(FirstName='Test 2',LastName='Test 2',Status='Open',Company='Test2',Sales_Cycle__c='O​pen') };
    insert leads;
    List<Event> events = new List<Event>{ new Event(WhoId=Leads[0].Id,Subject='Tour',ActivityDate=System.today(), startdatetime=system.now(), Durationinminutes=60),
                                          new Event(WhoId=Leads[1].Id,Subject='Something Else',ActivityDate=System.today(), startdatetime=system.now(), Durationinminutes=60) };
    insert events;
    Lead[] leadsgo = [select id,Sales_Cycle__c from lead where id in :leads];
    System.assertEquals('Open',Leadsgo[1].Sales_Cycle__c);
    
  }
}

 

 

And the trigger:

 

 

trigger tourchange on Event (before insert) {
  Set<Id> recordIds = new Set<Id>();
  List<Lead> leadsToUpdate = new List<Lead>();

  for(Event t:Trigger.new)
    if(t.subject=='tour' && t.Stages__c <> '30 days' && t.Stages__c <> '60 days')
      recordIds.add(t.whoid);

  leadsToUpdate = [select id from lead where id in :recordIds];

  for(Lead l:leadsToUpdate)
    L.sales_cycle__C = 'Tour Scheduled';

  update leadsToUpdate;
}

 

 

THANKS!

B

Best Answer chosen by Admin (Salesforce Developers) 
mtbclimbermtbclimber

Wow, that's not an easy one to decipher but I think I figured it out.  If you take the value of 'Open' that you have in the test where you create the lead and paste it into a character identifier resource like this:

 

http://software.hixie.ch/utilities/cgi/unicode-decoder/character-identifier

 

You'll see there are actually some apparently hidden characters in the value which are causing the equality check to fail. Not sure how you got that value in there but update your code by manually typing over the values with 'Open' in each location. Alternatively define a string variable and use it instead. Either change should make your test pass.

All Answers

mtbclimbermtbclimber

Wow, that's not an easy one to decipher but I think I figured it out.  If you take the value of 'Open' that you have in the test where you create the lead and paste it into a character identifier resource like this:

 

http://software.hixie.ch/utilities/cgi/unicode-decoder/character-identifier

 

You'll see there are actually some apparently hidden characters in the value which are causing the equality check to fail. Not sure how you got that value in there but update your code by manually typing over the values with 'Open' in each location. Alternatively define a string variable and use it instead. Either change should make your test pass.

This was selected as the best answer
BaguiarBaguiar

Wow for sure... Never thought of that and would be cooking my brain still. Thanks a lot as it worked beautifully after just typing 'Open'.  Before, I copied and pasted from the code itself... really have no clue why did it change characters..

 

Anyways, thanks a lot. having a issue with a very similar trigger and test method. Triied many logics on the System. assert  or  System.assertEquals, but not working. here is the test:

 

public class testautoassigntasktrigger {
  public static testMethod void test2() {
    List<Lead> leads = new List<Lead>{ new Lead(FirstName='Test 3',LastName='Test 1',Status='Open',Company='Test1',Sales_Cycle__c='Open'),
                                       new Lead(FirstName='Test 4',LastName='Test 2',Status='Open',Company='Test2',Sales_Cycle__c='Open') };
    insert leads;
    List<task> tasks = new List<task>{ new task(WhoId=Leads[0].Id,Subject='Get appointment',ActivityDate=System.today(), status='In Progress - Auto'),
                                          new task(WhoId=Leads[1].Id,Subject='Something Else',ActivityDate=System.today(), status='Not Started')};
    insert tasks;
    Lead[] leadsgo = [select id,sales_cycle__c from lead where id in :leads];
    task[] tasksgo = [select id, whoid from task where id in :tasks];
    System.assert(Leadsgo[0].owner = tasksgo[0].whoid);
  }
}

 

And the trigger:

 

 

trigger autoAssignTask on Task (before insert) {
  Set<Id> recordIds = new Set<Id>();
  Map<Id,Id> ownerIds = new Map<Id,Id>();

  for(Task t:Trigger.new)
    if(t.status=='In Progress - Auto' && t.whoid <> null)
      recordIds.add(t.whoid);

  for(Lead l:[select id,ownerid from lead where owner.type='user' and id in :recordIds])
    ownerIds.put(l.id,l.ownerid);

  for(Task t:Trigger.new)
    if(t.status=='In Progress - Auto' && t.whoid<>null && ownerIds.containsKey(t.whoid)<>null)
      t.ownerid = ownerIds.get(t.whoid);
      }

 

Thanks a lot. I've been learning Apex and all help is appreciated!

BaguiarBaguiar

Never mind.... used:

 

System.assertEquals(leadsgo[0].ownerid,tasksgo[0].whoid);

 

and it seems to work.

 

B