且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Node中模拟依赖项

更新时间:2023-11-23 19:43:22

您可以使用类似 nCore

老实说,这其中的难点实际上是模拟出mongoDB API,它是复杂且非琐碎的.我估计要花大约一周的时间才能模拟出我使用的大多数mongo API,因此我只需再次测试计算机上的本地mongodb数据库(始终处于怪异状态)

To be honest, the hard part of this is actually mocking out the mongoDB API, which is complex and non trivial. I estimate it would take about a week to mock out most of the mongo API I use so I just test againts the a local mongodb database on my machine (which is always in a weird state)

然后使用nCore特定语法

Then with nCore specific syntax

// myModule.js
module.exports = {
  myMethod: function () { 
    this.mongo.doStuff(...)
  },
  expose: ["myMethod"]
};

// test-myModule.js
var module = require("myModule")

module.mongo = mongoMock
assert(module.myMethod() === ...)