+ Start a Discussion
Abhijeet Purohit01Abhijeet Purohit01 

Problem in testclass

I have written a trigger to update a date/time field called Last_Stage_Change__c by system.now().

In the test class I used:

 

system.assertEquals(system.now(), obj.Last_Stage_Change__c);

 

Am getting an error msg saying

Expected: 10/4/2012 12:28:36pm, Actual: 10/4/2012 12:28:37pm.

 

 

That is the difference between Expected and Actual is one second.

 

What to do?

kibitzerkibitzer

Time passes between the time the field is set and the time of the test.

 

A safe approach would be to store the datetime at the start of the test in a variable. For example

 

DateTime initialtime = DateTime.Now();

 

Then, during the assert, make sure that the field is set to a DateTime larger than the stored date. That will validate that the field was set correctly.

 

system.assert(obj.Last_Stage_Change__c > initialtime);

 

Dan

 

Abhijeet Purohit01Abhijeet Purohit01

Am getting an error message: Assert Failed.

Rohit Kumar VermaRohit Kumar Verma

Dude,

Consideration in your scenarion.

 

Suppose you have assigned the [obj.Last_Stage_Change__c = Datetime.Now() ](At around 3:33)

and then you are assigned the [DateTime initialtime = DateTime.Now(); ](At around 3:33 + x_milli_seconds)

and finally you are trying to assert.

system.assert(obj.Last_Stage_Change__c > initialtime);

which will obviousally fail.

because Datetime only supports till seconds not bellow this.

try running

system.assert(obj.Last_Stage_Change__c  ==  initialtime);

 

 

SammyComesHereSammyComesHere

May be you can assert based on date.

 

Then Using hour and min you can assert. ignore the sec part

kibitzerkibitzer

Sorry - it was late

 

Should have been system.assert(obj.Last_Stage_Change__c >= initialtime);

 

Dan