Mat*_*ini 7 javascript testing mocha.js node.js
我正在为我正在构建的产品构建一些API测试.其中一个测试看起来像这样:
GET FILTERS
? should be restricted (45ms)
it should get the filters
? should return 200
? should return an object
? should close db connections
GET USERS COUNT
? should be restricted
? should throw error when payload is not correct
it should get the user count
? should return 200
? should return an object
? should close db connections
GET USERS FILE
? should be restricted
? should throw error when no queryId is specified
it should retrieve the file
? should return 200
? should download an excel file
? should close db connections
UPLOAD PROMOTION IMAGE
? should throw error when no file is specified
it should save the file
? should return 200
? should have named the file with the title of the promotion
? should have uploaded the file to S3 (355ms)
? should close db connections
CREATE PROMOTION
it should save the promotion
? should return 200
? should return a correct response
? should close db connections
GET PROMOTIONS
? should be restricted
it should get the promotions
? should return 200
? should be an array of promotions
? should contain the previously created promotion
UPDATE PROMOTION
it should update the promotion
? should return 200
? should return a correct response
? should close db connections
PUT PROMOTION IN BIN
it should put the promotion in the bin
? should return 200
? should return a correct response
? should close db connections
GET ARCHIVED PROMOTIONS
? should be restricted
it should get the promotions
? should return 200
? should be an array of promotions
? should be an array of archived promotions
? should contain the previously archived promotion
DELETE PROMOTION
it should delete the promotion
? should return 200
? should return a correct response
? should have deleted the file from S3 (563ms)
? should close db connections
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我已尝试将有关促销的所有内容放在单个测试套件中,以便我可以使用某种工作流来测试用户在平台上的确切操作.
在这个例子中,我创建了一个随机生成促销,然后使用该促销的ID来读取它,更新它,归档它然后最终删除它.每个步骤都是连接的,我需要为每个套装都有返回值(即:插入的促销的ID,或者过滤器......)
此时我的promotions.test.js文件是625,由于我还没有完成,我预计它会在接下来的几天内增长很多.
有没有办法在不同的文件中拆分多个测试套件,但每个测试/文件能够在完成后立即返回一个值,我可以传递给下一步?
编辑BOUNTY
目前我只尝试过这样的事情:
describe.only("Gifts Workflow", function() {
var createdGift;
describe("CREATE", function() {
require("./GIFTS/CREATE.js")().then(function(data) {
createdGift = data;
});
});
describe("READ FROM WEB", function() {
require("./GIFTS/READ FROM WEB.js")(createdGift).then(function(data) {
});
});
});
Run Code Online (Sandbox Code Playgroud)
内容为"./GIFTS/CREATE.js"
module.exports = function() {
return new Promise(function(resolve, reject) {
//DO SOME TESTS WITH IT AND DESCRIBE
after(function() {
resolve(createdGift);
});
});
Run Code Online (Sandbox Code Playgroud)
};
问题是测试立即由mocha初始化,因此在第二个测试套件"READ FROM WEB"中,作为createdGift传递的值立即给予测试,而不等待第一个测试完成,因此传递未定义.
这是我在我的代码中尝试的方式:
var create = require("./GIFTS/CREATE");
var read = require("./GIFTS/READ FROM WEB");
describe.only("Gifts Workflow", function() {
create(function(createdGift) {
read(createdGift);
});
});
Run Code Online (Sandbox Code Playgroud)
创建
module.exports = function(callback) {
var createdGift;
//SOME TESTS
describe("it should insert a gift", function() {
var result;
before(function() {
return request
.post(url)
.then(function(res) {
createdGift = res.body;
});
});
//SOME OTHER TESTS
});
after(function() {
callback(createdGift);
});
};
Run Code Online (Sandbox Code Playgroud)
从WEB阅读
module.exports = function(createdGift) {
it("should be restricted", function(done) {
request
.get(url)
.query({})
.end(function(err, res) {
expect(res.statusCode).to.equal(400);
done();
});
});
describe("it should read all gifts", function() {
//SOME TESTS
});
};
Run Code Online (Sandbox Code Playgroud)
这是输出
Gifts Workflow
? should be restricted
? should not work when incomplete payload is specified
it should insert a gift
? should return 200
? should return an object
? should have uploaded the image to S3 (598ms)
? should close db connections
it should read all gifts
? should return 200
? should return an array
? should contain the previously added gift
? should close db connections
10 passing (3s)
Run Code Online (Sandbox Code Playgroud)
它可能看起来有效但你可以从制表中看到它应该读取所有礼物不是礼品工作流的孩子,但是根套件的孩子.
这是发生的事情:
您是否可以通过两次测试确认这是您的情况?
before您在描述块中丢失了。下面的内容对我有用。
/test/helpers/gifts/create.js
\n\nmodule.exports = () => {\n const aGift = 'a gift';\n console.log('creating: ' + aGift);\n return Promise.resolve(aGift);\n};\nRun Code Online (Sandbox Code Playgroud)\n\n/test/helpers/gifts/read-from-web.js
\n\nmodule.exports = createdGift => {\n console.log('read-from-web got: ' + createdGift);\n return Promise.resolve('something read from the web');\n};\nRun Code Online (Sandbox Code Playgroud)\n\n/测试/index.js
\n\n//---------//\n// Imports //\n//---------//\n\nconst chai = require('chai')\n , create = require('./helpers/gifts/create')\n , readFromWeb = require('./helpers/gifts/read-from-web')\n ;\n\n\n//------//\n// Init //\n//------//\n\nchai.should();\n\n\n//------//\n// Main //\n//------//\n\ndescribe('gifts workflow', () => {\n\n var createdGift\n , somethingReadFromTheWeb\n ;\n\n describe('create', () => {\n before(() => create()\n .then(data => {\n createdGift = data;\n })\n );\n\n it('created gift should exist', () => {\n createdGift.should.equal('a gift');\n });\n });\n\n describe('read from web', () => {\n before(() => readFromWeb(createdGift)\n .then(data => {\n somethingReadFromTheWeb = data;\n })\n );\n\n it('something should be read from the web', () => {\n somethingReadFromTheWeb.should.equal('something read from the web');\n });\n });\n});\nRun Code Online (Sandbox Code Playgroud)\n\n结果输出(请原谅混乱)
\n\n$ mocha\n\n gifts workflow\n create\ncreating: a gift\n \xe2\x9c\x93 created gift should exist\n read from web\nread-from-web got: a gift\n \xe2\x9c\x93 something should be read from the web\n\n\n 2 passing (10ms)\nRun Code Online (Sandbox Code Playgroud)\n