AngularJS Jasmine 2.0异步测试超时

埃文·塞巴斯蒂安(Evan Sebastian)

我知道这个问题已经问过很多遍了,我已经看过其他问题并关注它们,但是我似乎无法解决这个问题。

基本上,我在Service中有一个函数可以将数据放入pouchDB中。该函数addTask将返回一个promise,当数据库插入成功时,promise将作为结果值解析。

在浏览器环境中的手动测试期间,此方法工作正常,但在茉莉花测试中,由于超时而无法通过。

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,但是没有用。

我是Lesh

这看起来是我能想到的两件事之一:

  1. 要么:您似乎并不是在单独测试Pouch服务。那个异步调用怎么回事?到底在做什么 有什么可以模拟并注入其中的东西,以便可以使用$q.when()$timeout.$flush()测试?
  2. ...或...您的意思是说这是一个集成测试,在这种情况下,您可以jasmine. DEFAULT_TIMEOUT_INTERVAL使用beforeEachafterEach

编辑:您的服务,如您的帖子中所写,以及所接受的答案无法单独进行测试。

如果要单独测试,则需要注入PouchDB作为依赖项。您无需测试Pouch,只需测试自己的代码即可:

# Create a service that gets PouchDB from $window
app.service 'PouchDB', ($window) ->
    return $window.PouchDB

# Now you can inject it as a dependency!
app.service 'Pouch', ($q, $rootScope, PouchDB) -> 
    # *now* new it up.
    db = new PouchDB 'tasks'

    return addTask: (task) ->
        deferred = $q.defer()

        db.put task, (task.title + task.due), (err, res) ->
          $rootScope.$apply ->  deferred.resolve res

        deferred.promise

现在,您的测试已被隔离:

describe 'Service: Pouch', ->
  Pouch = undefined
  $rootScope = undefined
  PouchDBMock = undefined
  $timeout = undefined
  value = undefined

  beforeEach () ->
    module 'testApp', ($provide) -> 
      # create your mock
      PouchDBMock = jasmine.createSpyObj('PouchDB', ['put', 'get', 'etc']);

      # provide it to the container
      $provide.value 'PouchDB', PouchDBMock


    # No longer necessary, because you're isolated!
    # ---> PouchDB.destroy 'tasks' <---

    # Now when you inject your Pouch service, it will have
    # our PouchDBMock being used inside of it.
    # also inject $timeout so you can flush() later
    inject (_Pouch_, _$rootScope_, _$timeout_) ->
      Pouch = _Pouch_
      $rootScope = _$rootScope_
      $timeout = _$timeout_

    # MOVED: this should be initialized in your beforeEach or your it, not in the body
    # of define... that's bad. Every new `it` test could mutate this and it won't reset
    # in the beforeEach where you had it.
    testTask =
      type: 'TASK'
      title: 'Feed the kitten'
      due: 201120141900
      group: ['TODAY', 'TOMORROW']




  it 'should add task upon request', (done) ->
    # tell the spy on `put` to return something distinct
    PouchDBMock.put.andReturn('something special');

    # call the method
    promise = Pouch.addTask testTask

    # handle the promise
    promise.then (result) ->
      # assert put was called on your mock
      expect(PouchDBMock.put).toHaveBeenCalledWith(testTask)
      # assert the result was what you expected (the return from the spy)
      expect(result).toBe('something special')
      # and you're done... even though this wasn't asynchronous
      done()

    # flush all unresolved promises
    $timeout.flush()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么我在 Jasmine 1.3.x 中的 AngularJS 异步测试不起作用?

来自分类Dev

Jasmine 2.0:异步测试用例

来自分类Dev

集成测试AngularJS + Karma + Jasmine

来自分类Dev

使用Jasmine测试AngularJS资源

来自分类Dev

AngularJS Jasmine测试init函数

来自分类Dev

为什么Jasmine在此异步测试中不执行it()?

来自分类Dev

循环内的Jasmine异步测试未按预期工作

来自分类Dev

Jasmine 异步测试错误处理

来自分类Dev

使用Karma,Jasmine和ngHtml2JsPreprocessor的AngularJS测试指令:未加载模板

来自分类Dev

如何使用Jasmine,AngularJs测试_.defer()

来自分类Dev

AngularJS Jasmine测试get-request

来自分类Dev

使用Karma(Jasmine)测试AngularJS工厂

来自分类Dev

在Jasmine 2.0中测试AngularJS Promise

来自分类Dev

AngularJs Jasmine单元测试中的$ httpBackend

来自分类Dev

AngularJS测试:Jasmine模拟回调

来自分类Dev

AngularJs通过Karma测试Jasmine抛出异常

来自分类Dev

AngularJS + Jasmine的单元测试滑块指令

来自分类Dev

AngularJS和Jasmine测试工厂

来自分类Dev

如何监视服务以进行测试-AngularJS / Jasmine

来自分类Dev

angular2测试框架异步测试问题

来自分类Dev

使用Jasmine 2.0在异步测试中获得“ $ digest已经在进行中”

来自分类Dev

无法设置回调时在Jasmine 2.0中进行异步测试

来自分类Dev

使用Jasmine 2.0在异步测试中获得“ $ digest已经在进行中”

来自分类Dev

带有Jasmine的单元测试Angularjs指令(包含私有超时)

来自分类Dev

带有Jasmine的单元测试Angularjs指令(包含私有超时)

来自分类Dev

Jasmine、AngularJS:对超时后返回值的函数进行单元测试

来自分类Dev

通过$ ular在AngularJS / karma / jasmine测试中测试后端API?

来自分类Dev

使用Jasmine 2的spyOn测试Angular指令

来自分类Dev

Angular 2/Jasmine 测试输入字段绑定

Related 相关文章

  1. 1

    为什么我在 Jasmine 1.3.x 中的 AngularJS 异步测试不起作用?

  2. 2

    Jasmine 2.0:异步测试用例

  3. 3

    集成测试AngularJS + Karma + Jasmine

  4. 4

    使用Jasmine测试AngularJS资源

  5. 5

    AngularJS Jasmine测试init函数

  6. 6

    为什么Jasmine在此异步测试中不执行it()?

  7. 7

    循环内的Jasmine异步测试未按预期工作

  8. 8

    Jasmine 异步测试错误处理

  9. 9

    使用Karma,Jasmine和ngHtml2JsPreprocessor的AngularJS测试指令:未加载模板

  10. 10

    如何使用Jasmine,AngularJs测试_.defer()

  11. 11

    AngularJS Jasmine测试get-request

  12. 12

    使用Karma(Jasmine)测试AngularJS工厂

  13. 13

    在Jasmine 2.0中测试AngularJS Promise

  14. 14

    AngularJs Jasmine单元测试中的$ httpBackend

  15. 15

    AngularJS测试:Jasmine模拟回调

  16. 16

    AngularJs通过Karma测试Jasmine抛出异常

  17. 17

    AngularJS + Jasmine的单元测试滑块指令

  18. 18

    AngularJS和Jasmine测试工厂

  19. 19

    如何监视服务以进行测试-AngularJS / Jasmine

  20. 20

    angular2测试框架异步测试问题

  21. 21

    使用Jasmine 2.0在异步测试中获得“ $ digest已经在进行中”

  22. 22

    无法设置回调时在Jasmine 2.0中进行异步测试

  23. 23

    使用Jasmine 2.0在异步测试中获得“ $ digest已经在进行中”

  24. 24

    带有Jasmine的单元测试Angularjs指令(包含私有超时)

  25. 25

    带有Jasmine的单元测试Angularjs指令(包含私有超时)

  26. 26

    Jasmine、AngularJS:对超时后返回值的函数进行单元测试

  27. 27

    通过$ ular在AngularJS / karma / jasmine测试中测试后端API?

  28. 28

    使用Jasmine 2的spyOn测试Angular指令

  29. 29

    Angular 2/Jasmine 测试输入字段绑定

热门标签

归档