我正在测试产品的添加,我想添加10个产品,为此,我使用https://www.npmjs.com/package/multer库。
我的测试:
describe('Test products', () => {
describe('POST /api/products/add', () => {
it(`Should create ${addProductCount} products with 0...1000 price`, (done) => {
let operationCount = addProductCount;
for (let i = 0; i < addProductCount; i++) {
let product = utils.getFakeProduct(2, 1000);
chai
.request(server)
.post('api/products/add')
.set('token', constants.merchantToken)
.field(product)
.attach('product_photos', './test/path/products/test1.jpeg', 'test1.jpeg')
.attach('product_photos', './test/path/products/test2.jpg', 'test2.jpg')
.attach('product_photos', './test/path/products/test3.png', 'test3.png')
.end((err, res) => {
operationCount--;
expect(res).have.status(200);
expect(res.body).have.property('message');
expect(res.body.message).to.be.equal('Product added');
if (operationCount == 0) {
done();
}
});
}
});
});
});
...
function getFakeProduct(lowerPrice, upperPrice) {
let currentDate = faker.date.recent();
let expirationDate = faker.date.recent(+1);
let original_price = getRandomInt(lowerPrice, upperPrice);
return {
product_name: faker.commerce.productName(),
product_description: `${faker.commerce.productAdjective()} ${faker.commerce.productAdjective()}`,
original_price,
sale_price: original_price - getRandomInt(lowerPrice, original_price - 1),
starting_date: currentDate,
end_date: expirationDate,
quantity_available: faker.random.number(50),
categories: 'HOME APPLIANCES',
};
}
执行测试时,出现错误:
TypeError:source.on不是函数
在Function.DelayedStream.create中(node_modules \ delayed-stream \ lib \ delayed_stream.js:33:10)
在FormData.CombinedStream.append(node_modules \ combined-stream \ lib \ combined_stream.js:44:37)
在FormData.append(node_modules \ form-data \ lib \ form_data.js:74:3)
在Test.RequestBase.field(node_modules \ superagent \ lib \ request-base.js:406:23)
在Test.RequestBase.field(node_modules \ superagent \ lib \ request-base.js:387:12)
在Context.field(test / productsTest.js:53:22)
在process.topLevelDomainCallback(domain.js:120:23)
我的问题是什么?我不使用此功能。