0

I am new to writing code and I spent some time writing the below trigger - now I get a code cover error when I try to move it to Production. I've tried writing some test code but can't get it right - any help would be appreciated - at the moment i keep getting "expecting right curly bracket, found ''" error in test code

Trigger code is here (it just time stamps related Account when Event is completed)

trigger Timestamper on Event (after insert, after update){
    List <ID> AccId = New List <ID>();

    for (Event e: trigger.new){
        if (e.Meeting_Status__c == 'Completed' && e.WhatId !=null){
            AccId.add(e.WhatId); 
        }
    }


    List <Account> accList = [SELECT id, Last_Event_Close_Date__c FROM Account WHERE id in:AccId];
    for(Account acnt : accList){
        acnt.Last_Event_Close_Date__c = System.today(); 
    }
    update accList;
}

Test code here

@isTest
private class Trig_Test_Timestamper
{
    static testMethod void Timestamper() {

    test.startTest();

    Account a = new Account (name = 'tester' );
    system.debug(Last_Event_Close_Date__c);
    insert a;

    Event e = new Event (name = 'test', Meeting_Status__c = 'Completed', WhatId = a.Id);  

    insert e;

    if (e.Meeting_Status__c = 'Completed')
    {
        set  (a.Last_Event_Close_Date__c = system.today() && e.WhatId = a.Id);  
    }

    a = [Select Last_Event_Close_Date__c from Account];


    system.assertEquals(Last_Event_Close_Date__c = system.today());

}

any help would be great thanks

new test code

@isTest
private class Trig_Test_Timestamper

{
static testMethod void Timestamper() {

    test.startTest();

Account a = new Account (name = 'tester' );
 system.debug(a.Last_Event_Close_Date__c);
 insert a;

Event e = new Event (Meeting_Status__c = 'Completed', WhatId = a.Id);  

insert e;

 a = [Select Last_Event_Close_Date__c from Account where id=:a.Id];
 system.debug(a.Last_Event_Close_Date__c = system.today());

 system.assert(a.Last_Event_Close_Date__c = system.today());

 }
}

new error "Method does not exist or incorrect signature :System asser(Date)"

  • thanks to you both - I think I have enough here to get working on - i'll revert when/if I sort this out - good weekend to ye all – user6866 Feb 14 '14 at 17:22
1

You have the generally right idea. In order to test a trigger, you need to perform some DML on that object. In a nutshell, if it's an insert trigger, you will need to at least insert the object. If it's an update, you need to perform an insert and an update on the sobject, etc.

You're also using System.assert, which believe it or not, a lot of bad unit tests leave out.

That being said, there are a few general things wrong with your code:

  1. Your trigger is not checking for an updated value of WhatId. Any time an event is updated, its account's Last Event Closed Date will be updated, regardless of if any relevant Event fields were updated.
  2. You have syntax errors in your test method. As others have pointed out, you're missing a curly brace, the = operator for this line should be == instead:

    e.Meeting_Status__c = 'Completed'

  3. There is no visible set method. Either you're just not showing this in your code snippet for the test class, or it simply doesn't exist.

  4. You're only using Test.startTest and not Test.stopTest

  5. You're only testing one condition in your test class. You should be testing insert, update, and negative conditions like where an update happens, but the WhatId and Meeting Status of the Event stay the same.

  6. You're not testing bulk Event inserts and updates. The gist is, you'll want to insert 200 Events instead of 1 event in your test method, testing that your Trigger logic can handle bulk data loads.

Here's a revamped version of your test class to hopefully get it to compile, with comments. Note, I broke it into three separate tests, one for insert, one for update, and one for the negative condition on update. I've also added in logic for testing your trigger in bulk.

@isTest
private class Trig_Test_Timestamper{

    static testMethod void testInsert() {


        Account a = new Account (name = 'tester' );
        system.debug(Last_Event_Close_Date__c);
        insert a;

        //Moved this to the actual DML that will cause the trigger to fire.  This will accurately test limits, etc.
        Test.startTest();

        //Fires trigger on insert, testing bulk load of 200 records
        List<Event> events = new List<Event>();
        for(Integer i=0; i < 200; i++){
            events.add(new Event(name='test'+i,Meeting_Status__c='Completed',WhatId=a.Id));
        }
        insert events; 
        Test.stopTest();

        a = [Select Last_Event_Close_Date__c from Account];

        System.assertEquals(Last_Event_Close_Date__c = system.today());

    }

    static testMethod void testUpdate() {

        //We're going to use a change in accounts to force an update on the event.
        List<Account> accts = new List<Account>();
        accts.add(new Account(Name='OrigAcct'));
        accts.add(new Account(Name='NextAcct'));
        insert accts;

        //Create event with original account, will update this to fire on update trigger
        List<Event> events = new List<Event>();
        for(Integer i = 0; i < 200; i++){
            events.add(new Event(name='test'+i,Meeting_Status__c='Completed',WhatId=a.Id));
        }
        insert events;

        for(Event e : events){
            e.WhatId = accts.get(1).Id;
        }
        Test.startTest();
        update events;
        Test.stopTest();

        List<Account> results = [Select Last_Event_Close_Date__c from Account];
        System.assertEquals(2,results.size());
        for(Account result : results){
            System.assertEquals(System.today(),result.Last_Event_Close_Date__c);
        }
    }

    /**
     * Test an update with events, where no change (nor update to the Last Event Close Date is needed
     */
    static testmethod void testUpdateWithNoChange(){
        //You'll want to test this as well, but will be tricky, because your date is based on System.today();
    }
}
|improve this answer|||||
0

Yes. You forgot to add your final } at end of your code. Try correctly indenting your code. So inside of your test method should be indented to the right further. That way you will notice the missing }. Also, on the line you are declaring your testMethod, you can put your opening { on the next line if it makes it easier for you visually like you did for your if statement.

Short version, you just need 1 more } at the end of your code.

Also, your assertion takes 2 arguments. You are assigning a variable to another inside to make a single argument instead of 2. It seems like you actually have several other generic coding errors in your code. Your code must be able to compile before you can deploy it.

|improve this answer|||||
  • cant believe i didnt see that! – user6866 Feb 14 '14 at 16:56
  • getting a new error now "Method does not exists or incorrect signature:system,asser(Date)" – user6866 Feb 14 '14 at 16:57
  • code changed to @isTest private class Trig_Test_Timestamper { static testMethod void Timestamper() { test.startTest(); Account a = new Account (name = 'tester' ); system.debug(a.Last_Event_Close_Date__c); insert a; Event e = new Event (Meeting_Status__c = 'Completed', WhatId = a.Id); insert e; a = [Select Last_Event_Close_Date__c from Account where id=:a.Id]; system.debug(a.Last_Event_Close_Date__c = system.today()); system.assert(a.Last_Event_Close_Date__c = system.today()); }} – user6866 Feb 14 '14 at 16:57
  • Don't know why you're getting a downvote for this, as it's the right answer! – Doug B Feb 14 '14 at 17:02
  • ehhh did I do that - sorry new to this - didnt even notice – user6866 Feb 14 '14 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.