Thursday, 12 July 2012

Remember to clear requests between jasmine-ajax tests

We use for . To , we rely on to spy, mock and fake. A basic usage pattern is to jasmine.Ajax.useMock() then check mostRecentAjaxRequest(), but remember to clearAjaxRequests() to avoid inconsistency (when running as part of a larger test suite).

Taking a look at the source, clearAjaxRequests() is not called within the code. When running several tests are run in the same browser page, stale requests from previous tests may be returned by mostRecentAjaxRequest().

So, instead of jasmine.Ajax.useMock() then check mostRecentAjaxRequest(), adopt this pattern:


  beforeEach ->
    clearAjaxRequests()
    ...

  afterEach ->
    ...
    clearAjaxRequests()

  it "should save the JSON data using Ajax", ->
    jasmine.Ajax.useMock()
    ...
    request = mostRecentAjaxRequest()
    ...

No comments:

Post a Comment