asynctestcase_test.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.testing.AsyncTestCaseTest');
  15. goog.setTestOnly('goog.testing.AsyncTestCaseTest');
  16. goog.require('goog.debug.Error');
  17. goog.require('goog.testing.AsyncTestCase');
  18. goog.require('goog.testing.asserts');
  19. goog.require('goog.testing.jsunit');
  20. function testControlBreakingExceptionThrown() {
  21. var asyncTestCase = new goog.testing.AsyncTestCase();
  22. // doAsyncError with no message.
  23. try {
  24. asyncTestCase.doAsyncError();
  25. } catch (e) {
  26. assertTrue(e.isControlBreakingException);
  27. assertEquals('', e.message);
  28. }
  29. // doAsyncError with string.
  30. var errorMessage1 = 'Error message 1';
  31. try {
  32. asyncTestCase.doAsyncError(errorMessage1);
  33. } catch (e) {
  34. assertTrue(e.isControlBreakingException);
  35. assertEquals(errorMessage1, e.message);
  36. }
  37. // doAsyncError with error.
  38. var errorMessage2 = 'Error message 2';
  39. try {
  40. var error = new goog.debug.Error(errorMessage2);
  41. asyncTestCase.doAsyncError(error);
  42. } catch (e) {
  43. assertTrue(e.isControlBreakingException);
  44. assertEquals(errorMessage2, e.message);
  45. }
  46. }
  47. function testMaybeFailTestEarly() {
  48. var message = 'Error in setUpPage().';
  49. var asyncTestCase = new goog.testing.AsyncTestCase();
  50. asyncTestCase.setUpPage = function() { throw Error(message); };
  51. asyncTestCase.addNewTest('test', function() { assertTrue(true); });
  52. asyncTestCase.runTests();
  53. window.setTimeout(function() {
  54. assertFalse(asyncTestCase.isSuccess());
  55. var errors = asyncTestCase.getResult().errors;
  56. assertEquals(1, errors.length);
  57. assertEquals(message, errors[0].message);
  58. }, asyncTestCase.stepTimeout * 2);
  59. }