【问题标题】:Node.js: test assert that a pointer points to myType, not only to objectNode.js:测试断言指针指向 myType,而不仅仅是对象
【发布时间】:2014-03-24 12:08:12
【问题描述】:

我有一个“UserService”对象,它只包含以下功能:

  • exports.usersList
  • exports.insertUser
  • exports.registerUser
  • 等等……

在我的测试文件中:

var userController = serviceFactory.getUserService();

现在,我必须断言 userController 指向 UserService 但如果我这样做:

typeof userController

它只返回object 类型,而不是我想要的“UserService”。 如何通过 node.js 断言检查它?

【问题讨论】:

  • 如何创建服务? instanceof 不适合你吗?

标签: javascript node.js assert typeof


【解决方案1】:

如果你定义了一个类(即 JS 中的函数),试试instanceof

// in your user_controller.js file
Function UserController() {
  ...
}
UserController.prototype.userList = function() {
  ...
}
module.exports = UserController

// in another file
var userController = new UserController();
userController instanceof UserController // prints true

【讨论】:

    猜你喜欢
    • 2020-12-06
    • 1970-01-01
    • 2020-08-17
    • 2011-07-05
    • 2010-11-23
    • 1970-01-01
    • 2019-01-22
    相关资源
    最近更新 更多