实验室报告<覆盖所有路径的100%覆盖范围

时间:2015-08-30 00:47:03

标签: javascript node.js hapijs lab

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

snapshot361

我希望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()
  })
})

0 个答案:

没有答案