0

我有一项服务需要通过 AJAX 调用数据库,这是对它的测试:

describe 'Cep service', ->
  cep = undefined
  $timeout = undefined

  beforeEach(module('cep'))
  beforeEach(inject(($cep, $injector)->
    cep = $cep
    $timeout = $injector.get('$timeout')
  ))

  afterEach(->
    $timeout.verifyNoPendingTasks()
  )

  it 'should fail', (done) ->
    cep(
      '00000000' 
      (data) ->
        expect(->
          throw new Error('Shouldnt succeed')
        ).to.not.throwError()
        return
      (err) ->
        expect(err).to.be.ok()
        expect(err).to.be('CEP inválido')
        done()
        return
    )

    try
      $timeout.flush()
    catch e
      console.log e

    return

  it 'should work with dashes', (done) ->
    cep(
      '91010-000'
      (data) ->
        expect(data).to.be.ok()
        expect(data).to.have.property('resultado')
        expect(data).to.have.property('uf')
        expect(data).to.have.property('cidade')
        expect(data).to.have.property('bairro')
        expect(data).to.have.property('tipo_logradouro')
        expect(data).to.have.property('logradouro')
        done()
        return
      (err) ->
        expect(->
          throw new Error('Shouldnt fail')
        ).to.not.throwError()
        done()
        return
    )
    try
      $timeout.flush()
    catch e
      console.log e

    return


  it 'should work without dashes', (done) ->
    cep(
      '91010000'
      (data) ->
        expect(data).to.be.ok()
        expect(data).to.have.property('resultado')
        expect(data).to.have.property('uf')
        expect(data).to.have.property('cidade')
        expect(data).to.have.property('bairro')
        expect(data).to.have.property('tipo_logradouro')
        expect(data).to.have.property('logradouro')
        done()
        return
      (err) ->
        expect(->
          throw new Error('Shouldnt fail')
        ).to.not.throwError()
        done()
        return
    )

    try
      $timeout.flush()
    catch e
      console.log e

    return

因错误而失败(因为done从未调用过 mocha):

Chrome 33.0.1750 (Windows 7) Cep service should fail FAILED             
    Error: timeout of 2000ms exceeded
        at C:/project/node_modules/mocha/mocha.js:4290:14

我打电话不打电话$timeout.flush都没关系。当我在调用$timeout.flush后调用时(因为 AJAX 尚未返回),它会引发错误并记录到 console No deferred tasks to flush

我已经加载angular-mocks.js了我的karma.conf.js并且它们是相同的版本(角度和模拟,使用保持最新bower update)。

在 CEP 服务内部,调用 ajax 后,回调在 a 内部执行,$timeout(-> cb(data))并且无论出于何种原因都不会调用。我难住了。

我在 SO 中看到的所有问题都与 e2e、jasmine 和使用$httpBackend模拟的同步测试有关。我需要实际调用 PHP 脚本进行测试。另外,我已经设置了“代理”karma.conf.js

proxies       : {
  '/planos': 'http://local.gap/planos',
  '/ajax'  : 'http://local.gap/ajax'
},

开发工具控制台显示请求正在完成但它正在停止$timeout

angular.module('cep', ['pheryResource'])
  .factory(
    '$cep'
    ['$pheryResource', (ajax) ->
      (cep, cb, err) ->
        cep = cep?.replace /[^0-9]*/g, ''

        if not cep? or cep.length < 8
          err?('CEP inválido')
          return

        ajax('cep', '/ajax').get(
          {cep: cep}
          (data) ->
            if not data? or not data.resultado?
              err?('CEP inválido')
            else
              cb(data)

            return
          (error) ->
            err?(error)

            return
        )

        return
    ]
  )

angular.module('pheryResource', [])
  .factory(
    '$pheryResource'
    ['$timeout', ($timeout) ->
      phery_remote = (remote, url) ->
        @element = phery.element(remote, if url then {target: url} else undefined)
        angular.extend @, @element.phery()
        return

      event = (element, cb, err) ->
        element.off('phery:json phery:fail').on(
          'phery:json': (event, data) ->
            $timeout(->
              cb?(data)
              return
            )
            return

          'phery:fail': (event, error) ->
            $timeout(->
              err?(error)
              return
            )
            return
        )

      phery_remote::get = (data, cb, err) ->
        @element.phery('data', 'method', 'GET')
        event(@element, cb, err)
        @element.phery('one', data)

      phery_remote::post = (data, cb, err) ->
        @element.phery('data', 'method', 'POST')
        event(@element, cb, err)
        @element.phery('one', data)

      phery_remote::put = (data, cb, err) ->
        @element.phery('data', 'method', 'PUT')
        event(@element, cb, err)
        @element.phery('one', data)

      phery_remote::delete = (data, cb, err) ->
        @element.phery('data', 'method', 'DELETE')
        event(@element, cb, err)
        @element.phery('one', data)

      (remote, url) ->
        new phery_remote(remote, url)
    ]
  )
4

0 回答 0