[SalesForce] Test class: Assertion Failed

I m trying to cover and test my apex code so I developped a test class to test my insertion, my the test is failed unfortulately; and gave me this error: Assertion error

here is my test class:

@isTest
private class TestTimeOff {

    static testMethod void myTimeOffTest() {
        Test.startTest();
         Time_Off__c to = new Time_Off__c();
            to.Start_date__c = Date.newInstance(12, 05, 2015);
            to.End_date__c = Date.newInstance(13, 05, 2015);
            //to.My_back_up__c = 'XXXX';
            to.Pattern__c = 'Payed time off';
            to.Comment__c='My Class Test';
      try{
             insert to;
        }
        catch(System.DMLException e){

        System.assert(e.getMessage().contains('Insertion failed'));
       }
       test.stopTest();
 }

}

an other problem; My_back_up__c is a reference to a user, how can I do this? should I give the user ID??

p.s. I have trigger launched before and after adding a Time Off

Thank you

Best Answer

You can use this for create a test user, in your test method.

    User u = new User(
                   FirstName = 'test',
                   LastName = 'test',
                   UserName = 'test@test.com.asdf',
                   Email = 'test@test.com',
                   ProfileId = UserInfo.getProfileId(),
                   Alias = 'TEST',
                   CommunityNickname = 'test.test',
                   EmailEncodingKey = 'ISO-8859-1',
                   LocaleSidKey = 'es_ES',
                   LanguageLocaleKey = 'en_US',
                   TimeZoneSidKey = 'Europe/Paris',
                   IsActive = true
                );
   insert u;

Can you tell us what is doing your trigger? i dont understand your assertion.

Related Topic