我知道这已经被问过很多次了,我已经查看了其他问题并关注了它们,但我似乎无法解决这个问题。
基本上,我在 Service 中有一个函数可以将数据放入 pouchDB。该函数addTask
将返回一个promise,当数据库插入成功时,该promise 将解析为结果值。
这在浏览器环境中的手动测试期间工作正常,但在 Jasmine 测试期间由于超时而失败。
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
似乎在then
规范中作为参数传递的回调永远不会运行。
app = angular.module 'testApp', ['ngMock']
app.service 'Pouch', ($q) ->
db = new PouchDB 'tasks'
return {
addTask : (task) ->
deferred = $q.defer()
db.put task, (task.title + task.due), (err, res) ->
console.log res # Both prints fine
console.log err
deferred.resolve res
return deferred.promise
}
describe 'Service: Pouch', ->
Pouch = {}
$rootScope = {}
beforeEach () ->
module 'testApp'
PouchDB.destroy 'tasks'
inject (_Pouch_, _$rootScope_) ->
Pouch = _Pouch_
$rootScope = _$rootScope_
value = undefined
testTask =
type: 'TASK'
title: 'Feed the kitten'
due: 201120141900
group: ['TODAY', 'TOMORROW']
it 'should add task upon request', (done) ->
promise = Pouch.addTask testTask
promise.then (result) ->
# Never reached here
expect(result.ok).toBe(true)
done()
$rootScope.$apply() # I don't think this is neccessary.
我该怎么办?我也试过用$timeout
,但没有用。