2

我有一个测试来检查服务器是否配置正确。服务器和测试的代码都在要点中。但是,运行lab --environment TEST --verbose --coverage --reporter console --output stdout --reporter html --output coverage给了我这个(提供了 HTML 版本):

快照361

我想为这个文件获得 100% 的覆盖率。为了完整起见,我在行中包含了代码段:

应用程序/server.js

const initializerNames = ['config', 'db', 'auth']
const Hapi = require('hapi')
let server = new Hapi.Server()

function applyInitializerToServer (initializerName) {
  const functor = require('./server/' + initializerName + '.js')
  const modifiedServer = functor(server)
  server.log('Setting up initializer for ' + initializerName + '...')

  if (modifiedServer === undefined) {
    throw new ReferenceError('Server malformed.')
  } else {
    server = modifiedServer
  }
}

module.exports = function implementServer () {
  server = new Hapi.Server()
  initializerNames.forEach(applyInitializerToServer)
  return server
}

测试/server.js

const testHelper = require('./helper.js')
const lab = exports.lab = testHelper.Lab.script()

lab.experiment('server', function () {
  const assert = testHelper.Chai.assert
  const Proxyquire = testHelper.Proxyquire
  const serverPath = testHelper.rootPath + '/app/server.js'

  lab.test('returns object if configured properly', function (done) {
    const mockServer = require(serverPath)

    assert.doesNotThrow(mockServer, 'server can be set up')
    assert.isObject(mockServer(), 'server was obtained as object')
    done()
  })

  lab.test('returns undefined if configured improperly', function (done) {
    const mockedModule = {
      './server/db.js': function (server) { return undefined }
    }
    const mockServer = Proxyquire(serverPath, mockedModule)

    assert.throws(mockServer,
                  ReferenceError, 'malformed',
                  'throws error about malformed server')
    done()
  })
})
4

0 回答 0