سؤال

I want to test that my web app handles a failure scenario and displays the corresponding error message using QUnit.

My app performs a JSONP request to get its initialization contents. I want to test the case where my Data Source is unavailable upon startup. This scenario results in an error on the underlying JQuery code, which I believe QUnit is intercepting and capturing it as a failed assertion, even though that is the expected behavior.

My app code looks like this:

loadContents() {
    $.ajax({
        url: initURL,
        dataType: 'jsonp',
        timeout: timeout,
        success: initializeDisplay,
        error:   displayErrorMessage,
    });
}

My test case looks like this:

asyncTest("Invalid initialization host", 1, function() {
    initURL = invalidURL;
    loadPageContents();
    index = $("#error-message-div").text().indexOf(errorMessage);
    ok(index > -1, "Error message not found");
})

When I run my test, I get an extra assertion, which always fails. The assertion contains the message: "Error loading script" with a URL to the JSONP GET request.

The issue I believe I am running into is that since the JSONP callback is never executed, it throws an exception which QUnit believes to be a failed assertion.

Any ideas how I could get my test to execute without registering the exception as a failure?

Update - To clarify, the QUnit assertion works as expected, however I am somehow getting an additional assertion, which fails always.

Thanks!

هل كانت مفيدة؟

المحلول

Knowing that the cause was that the JSONP call was failing on the background, in order to suppress this error during testing I had to add an error handler to the document object when the error was expected.

  errorNamespace = "error.test"
  $(document).on(errorNamespace, function(event) {
      ok(true, "Received expected error");
      // Prevent the error from propagating into the assertion
      event.stopPropagation();
      // Remove the error handler to allow other errors to flow
      $(document).off(errorNamespace);
  });

Unfortunately, the error event does not contain any information that would help me verify that the caught error is the one I expected, which can be troublesome.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top