【发布时间】:2015-11-23 11:04:03
【问题描述】:
asynFn(url, callback)
这个函数接受一个 url 并触发一些 xhr 请求,然后使用callback(result) 发回处理后的结果。我应该如何测试它?
(我已经直接在 Chrome 中运行了asynFn,它运行良好。)
我尝试使用jasmine-ajax 来存根请求,但expect 不起作用。
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
})
})
我在这里错过了什么?
【问题讨论】:
-
请贴出被测代码并使用分号!
-
@Sonata 哦,对不起。因为我只想知道如何测试这种功能。所以我认为没有必要在这里发布所有内容。
标签: javascript ajax testing jasmine bdd