function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nil_von_9wonil_von_9wo 

Problem with Force.com_Workbook v2: Tutorial 6: Testing Negative Test Cases

Page 39 of Force.com_workbook v2. provides code for testing the class created in Tutorial 5.

No surprisingly, this test code itself is wrong, and running it fails.

How do I correct the code so that these two asserts will work correctly?

System.assert(e.getMessage().contains('Insert failed. First exception on row 0; '+
'first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, '+
'Mileage request exceeds daily limit(500): [Miles__c]'),
e.getMessage());

System.assertEquals(Mileage__c.Miles__c, e.getDmlFields(0)[0]);

I've already discovered that I can get the first assertion to work correctly if change "...daily limit(500): [Miles__c]')," to "daily limit: 500: []')," ... but I imagine that "Miles__c" should have some value that isn't being added in MileageUtil, but should?

And the second assertion, I have no idea how to fix....

-Brian.
Best Answer chosen by Admin (Salesforce Developers) 
tendonstrengthtendonstrength

I was working on these same examples today and ran across this thread. I know it has been a while, but I figured I would post the solution in case someone else has the same problem when working through the examples.

 

The error here is not in the code for the tests, but in the  MileageUtil class that is being tested. In the areMilesAllowed method the code in the workbook shows the following line for creating the error message:

 

m.addError('Mileage request exceeds daily limit: ' + MAX_MILES_PER_DAY);

 

This line should instead read:

 

m.Miles__c.addError('Mileage request exceeds daily limit: ' + MAX_MILES_PER_DAY);

 

This way it specifies which field is throwing the error.  If this line is corrected the test case works as shown in the workbook example.

 

 

 

 

All Answers

tendonstrengthtendonstrength

I was working on these same examples today and ran across this thread. I know it has been a while, but I figured I would post the solution in case someone else has the same problem when working through the examples.

 

The error here is not in the code for the tests, but in the  MileageUtil class that is being tested. In the areMilesAllowed method the code in the workbook shows the following line for creating the error message:

 

m.addError('Mileage request exceeds daily limit: ' + MAX_MILES_PER_DAY);

 

This line should instead read:

 

m.Miles__c.addError('Mileage request exceeds daily limit: ' + MAX_MILES_PER_DAY);

 

This way it specifies which field is throwing the error.  If this line is corrected the test case works as shown in the workbook example.

 

 

 

 

This was selected as the best answer
IanM1963IanM1963

I'm having a similar problem. However, I have changed the code in MileageUtil.cls to no avail as when I re-run the test it still fails exactly as before. This is puzzling me now...