使用sinon模拟猫鼬数据库

时间:2019-10-21 20:23:26

标签: mongoose sinon

我正在审查一些测试中发现的一些错误。
已更新!

猫鼬npm软件包的版本是5.5.5

我删除了未正确使用的模拟 例如,他们在不使用withArgs的情况下调用了chain('sort')

.withArgs('-date')

这消除了指出以下内容的错误消息:

 TypeError: Cannot read property 'events' of undefined

现在,我遇到了新的错误

   should made Mongoose document methods chainables
         #verify chained:
     Error: should fail to bookMock.verify()

我已经更新了测试,现在通过了。我需要知道两个测试是否等效。

 it('Verify chained - expectation withArgs()', function(done) {
      var bookMock = sinon.mock(new Book({ title: 'Rayuela' }))

      bookMock
        .expects('update')
        .chain('sort')
        .withArgs({ field: 'asc' })
        .chain('exec')
        .resolves('RESULT')

      bookMock.object
        .update('SOME_ARGUMENTS')
        .sort({ field: 'asc' })
        .exec()
        .then(function() {
          // eslint-disable-line
          try {
            bookMock.verify()
            sinon.restore()
            done(new Error('should fail to bookMock.verify()'))

重构后,这是相同的测试:

it('Verify chained - expectation withArgs()', function(done) {
      var bookMock = sinon.mock(new Book({ title: 'Rayuela' }))
      var error = new Error('should fail to bookMock.verify()')
      bookMock
        .expects('update')
        .throws(error)
        .chain('sort')
        .withArgs({ field: 'asc' })
        .chain('exec')
        .resolves('RESULT')

      bookMock.object
        .update('SOME_ARGUMENTS')
        .sort({ field: 'asc' })
        .exec()
        .then(function() {
          // eslint-disable-line
          try {
            bookMock.verify()
            sinon.restore()
            done()

感谢您提供的任何帮助!

0 个答案:

没有答案