lundi 23 novembre 2015

Don't know how to test this asyn function with Jasmine

asynFn(url, callback)

This function takes a url and fires some xhr requests, then uses callback(result) to send back processed result. How should I test it?

(I've run the asynFn directly in Chrome and it worked fine.)

I tried to use jasmine-ajax to stub the request, but the expect did't work.

describe('a test', function() {
  var callback

  beforeAll(function() {
    jasmine.Ajax.install()

    jasmine.Ajax.stubRequest('fake/path1').andReturn({
      status: 200,
      contentType: 'text/plain',
      responseText: 'yay'
    })

    jasmine.Ajax.stubRequest('fake/path2').andReturn({
      status: 200,
      contentType: 'text/plain',
      responseText: 'yay2'
    })

    // ...
  })

  afterAll(function() {
    jasmine.Ajax.uninstall()
  })

  beforeEach(function() {
    callback = jasmine.createSpy('sendResponse')
  })

  it('a spec', function() {

    asynFn('input string', callback)

    expect(jasmine.Ajax.requests.mostRecent().url).toBe('fake/path2')
    expect(callback).toHaveBeenCalled() // faild
  })
})

What am I missing here?

Aucun commentaire:

Enregistrer un commentaire