+ Start a Discussion
RelaxItsJustCodeRelaxItsJustCode 

Help Please, having test code issues???

trigger CaseCloseTrigger on Case (after update){
        for(Case c : [SELECT id, Status FROM Case WHERE id in:trigger.new and Case.RecordTypeId = '0123000000001l1']){           
    if(c.Status == 'Completed' || c.Status == 'Closed'){                       
for(Training_Information__c customobj :
[Select Id, Status__c From Training_Information__c Where Case__c in: Trigger.newMap.keySet()]){
if(customobj.Status__c == 'Pending' || customobj.Status__c == 'Scheduled'){ trigger.new[0].addError('Training has not been Completed.');
}} } }}

 

Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

 

The following steps need done in your test method:

1) Create a case

2) Create a Training_Information__c object that is associated to this case with a status of 'Pending'

3) Update your case to have a Status of 'Completed'

NOTE-Put this in a try/catch block because it WILL fail.  This failure is on purpose since this is what you are trying to hit in your test.

4) Try to get in the habit of using System.assert to ensure that the things are happening that you expect.  This is NOT the way you would typically use an assert, but it works here because you are trying to force an error to happen.

try
{
  update yourCase;
  System.assert(false, 'I expect to never reach this assert since the trigger should cause an error on update.');
}catch(Exception e){}

 

All Answers

Damien_Damien_

2 things...

 

1) NEVER hardcode Ids

2) Don't put all your logic on a single line... nobody is going to bother reading your code, and if they're forced to it will be a major headache for them.  Also use the 'insert code' button for code...

RelaxItsJustCodeRelaxItsJustCode

It's a recordtypeid and if you have a better way instead of preaching you might lead by example.

Damien_Damien_
Id recordTypeId = [SELECT Id FROM RecordType WHERE Name = 'RecordTypeName' AND SObjectType = 'Case'].Id;

 

RelaxItsJustCodeRelaxItsJustCode

Ok that I can appriciate.  But it is still a working trigger forgive the syntax but it was one of my first 2 triggers ever.  So Damien, how would you write test code for this?

Damien_Damien_

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

 

The following steps need done in your test method:

1) Create a case

2) Create a Training_Information__c object that is associated to this case with a status of 'Pending'

3) Update your case to have a Status of 'Completed'

NOTE-Put this in a try/catch block because it WILL fail.  This failure is on purpose since this is what you are trying to hit in your test.

4) Try to get in the habit of using System.assert to ensure that the things are happening that you expect.  This is NOT the way you would typically use an assert, but it works here because you are trying to force an error to happen.

try
{
  update yourCase;
  System.assert(false, 'I expect to never reach this assert since the trigger should cause an error on update.');
}catch(Exception e){}

 

This was selected as the best answer
RelaxItsJustCodeRelaxItsJustCode

Hold on one sec Demien, I have to put on my CEO hat for a few.  Thats right, CEO, long story......  But I will get right back with you...  and in case you are wondering no matter what level your at in a company it's always good to sharpen the saw and learn new things which is what I'm doing now. 

RelaxItsJustCodeRelaxItsJustCode
@isTest
private class ctrainingvalidation {

    static testMethod void myUnitTest() {
        Case c = new Case();
		c.Status = 'Completed';
		c.RecordTypeId = '0123000000001l1';
		c.Est_Hours_to_Implement__c = 1;
		c.Total_Actual_Hours_to_Date__c = 1;
insert c;
}}

 Ok Damien, so I use code like  above but I set the c.Status to 'On Track',

 

Then I go and create a training record that is associated to this case with a Stage of "Pending" the same way I did above,

 

Then I do an update to the case trying to set it's c.Status to complete.

 

What I don't get is the try/catch thing where does it go the test code or the trigger code?

Damien_Damien_

Try/catch goes around the update in the test code.  You will be expecting an error when it executes.  Don't hardcode the RecordTypeId.  I have had instances where the RecordTypeId has remained the same from Dev to Sandbox to Production, and other times it has changed.  If it changes, it will not allow you to deploy your code to production because your test class will fail.

RelaxItsJustCodeRelaxItsJustCode

Thank you Damien.