【发布时间】:2014-09-23 20:57:48
【问题描述】:
我知道这个问题已经被问过很多次了,我已经查看了其他问题并关注了他们,但我似乎无法解决这个问题。
基本上,我在 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,但是没有用。
【问题讨论】:
标签: angularjs coffeescript jasmine phantomjs pouchdb