Skip to content

Commit 26c7ffa

Browse files
committed
add documentation about testing errors
1 parent 79acb71 commit 26c7ffa

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

Documentation/Contributors/TestingGuide/README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,22 +441,34 @@ In addition to testing success cases, we also test all failure cases. The custom
441441
```javascript
442442
it("fromDegrees throws with no latitude", function () {
443443
expect(function () {
444-
Cartesian3.fromDegrees(0.0);
445-
}).toThrowDeveloperError();
444+
Cartesian3.fromDegrees(0.0, undefined);
445+
}).toThrowDeveloperError(
446+
"Expected latitude to be typeof number, actual typeof was undefined",
447+
);
446448
});
447449
```
448450

449451
Above, `Cartesian3.fromDegrees` is expected to throw a `DeveloperError` because it expects longitude and latitude arguments, and only longitude is provided.
450452

451-
Tips:
453+
#### Tips
454+
455+
- When testing for exceptions it is recommended to test for the expected error message to verify that the test is triggering the correct error. This can be achieved either with the full error message, like above, or with a regular expression that will match the error message like this:
456+
457+
```javascript
458+
it("fromDegrees throws with no latitude", function () {
459+
expect(function () {
460+
Cartesian3.fromDegrees(0.0, undefined);
461+
}).toThrowDeveloperError(/Expected latitude to be/);
462+
});
463+
```
452464

453465
- When testing for exceptions, put only code that is expected to trigger the exception inside the function passed to `expect()`, in case setup code unintentionally throws an exception.
454466
- To verify the right exception is thrown, it is often useful to comment out the `expect` call when first running the test, for example:
455467

456468
```javascript
457469
it("fromDegrees throws with no latitude", function () {
458470
// expect(function() {
459-
Cartesian3.fromDegrees(0.0);
471+
Cartesian3.fromDegrees(0.0, undefined);
460472
// }).toThrowDeveloperError();
461473
});
462474
```

packages/engine/Specs/Core/Cartesian3Spec.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,14 +1241,16 @@ describe("Core/Cartesian3", function () {
12411241

12421242
it("fromDegrees throws with no longitude", function () {
12431243
expect(function () {
1244-
Cartesian3.fromDegrees();
1245-
}).toThrowDeveloperError();
1244+
Cartesian3.fromDegrees(undefined, undefined);
1245+
}).toThrowDeveloperError(/Expected longitude to be/);
12461246
});
12471247

12481248
it("fromDegrees throws with no latitude", function () {
12491249
expect(function () {
1250-
Cartesian3.fromDegrees(1);
1251-
}).toThrowDeveloperError();
1250+
Cartesian3.fromDegrees(1, undefined);
1251+
}).toThrowDeveloperError(
1252+
"Expected latitude to be typeof number, actual typeof was undefined",
1253+
);
12521254
});
12531255

12541256
it("fromDegrees works works with default ellipsoid", function () {

0 commit comments

Comments
 (0)