Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 测试失败-ProductService不是构造函数_Javascript_Node.js_Mocha.js_Chai - Fatal编程技术网

Javascript 测试失败-ProductService不是构造函数

Javascript 测试失败-ProductService不是构造函数,javascript,node.js,mocha.js,chai,Javascript,Node.js,Mocha.js,Chai,我使用的是“chai”:“^4.2.0”、和“摩卡”:“^4.0.1”。我正在运行节点--version,v10.15.3我的目标是测试服务层: 我的ProductService.js如下所示: class ProductService { constructor() { // constructor } async createOrUpdateProduct(dataArray) { return "done" } } mo

我使用的是
“chai”:“^4.2.0”、
“摩卡”:“^4.0.1”
。我正在运行
节点--version
v10.15.3
我的目标是测试服务层:

我的
ProductService.js
如下所示:

class ProductService {

    constructor() {
        // constructor
    }

    async createOrUpdateProduct(dataArray) {
        return "done"
    }
}

module.exports = {
    ProductService
};
const assert = require('chai').assert;

const ProductService = require('../Service/ProductService')


describe('Product model', () => {

    it('should add the test data with the Products Service to the Product table', async () => {
        let dataArr = "product data"
        let productServ = new ProductService()

        const res = await productServ.createOrUpdateProduct(dataArr)
        assert.isOk(res.length, dataArr.length);
    });

});
我的测试类
ProductTestService.js
如下所示:

class ProductService {

    constructor() {
        // constructor
    }

    async createOrUpdateProduct(dataArray) {
        return "done"
    }
}

module.exports = {
    ProductService
};
const assert = require('chai').assert;

const ProductService = require('../Service/ProductService')


describe('Product model', () => {

    it('should add the test data with the Products Service to the Product table', async () => {
        let dataArr = "product data"
        let productServ = new ProductService()

        const res = await productServ.createOrUpdateProduct(dataArr)
        assert.isOk(res.length, dataArr.length);
    });

});
运行测试时,我得到:

有什么建议说明实例化为什么不起作用吗

谢谢你的回复

代码

module.exports={
产品服务
};
是的缩写

module.exports={
ProductService:ProductService
};
这意味着,当您使用

const ProductService=require('../Service/ProductService');
ProductService
的值正是您导出的值,即具有属性ProductService的对象

{
ProductService:ProductService
}
若要解决您的问题,请直接导出类(如果它是您希望从模块导出的唯一内容)

module.exports=ProductService;
或者,如果您还想导出其他内容,请使用对象分解进行导入

const{ProductService}=require('../Service/ProductService');
代码

module.exports={
产品服务
};
是的缩写

module.exports={
ProductService:ProductService
};
这意味着,当您使用

const ProductService=require('../Service/ProductService');
ProductService
的值正是您导出的值,即具有属性ProductService的对象

{
ProductService:ProductService
}
若要解决您的问题,请直接导出类(如果它是您希望从模块导出的唯一内容)

module.exports=ProductService;
或者,如果您还想导出其他内容,请使用对象分解进行导入

const{ProductService}=require('../Service/ProductService');