gpt4 book ai didi

node.js - 循环 Mocha 测试

转载 作者:搜寻专家 更新时间:2023-10-31 22:39:14 24 4
gpt4 key购买 nike

我正在尝试使用 mocha 中的数据提供程序来编写更少的代码

var should = require('should'); 
var assert = require('assert');
var request = require('supertest');
var mongoose = require('mongoose');
var winston = require('winston');
var config = require('../app/config');

describe('Authentification', function() {
var url = config.web.protocol + '://' + config.web.host + ':' + config.web.port;

describe('signin',function()
{
var provider = [
{
describe: 'should return error trying to signin with empty body',
body: {},
status: 404,
message: "firstName not found"
},
{
describe: 'should return error trying to signin with no first name',
body: {
lastName: 'test',
password: 'test',
email: 'test'
},
status: 404,
message: "firstName not found"
},
{
describe: 'should return error trying to signin with no last name',
body: {
firtsName: 'test',
password: 'test',
email: 'test'
},
status: 404,
message: "lastName not found"
},
{
describe: 'should return error trying so signin with no password',
body: {
lastName: 'test',
firstName: 'test',
email: 'test'
},
status: 404,
message: "password not found"
},
{
describe: 'should return error trying so signin with no email',
body: {
lastName: 'test',
password: 'test',
firstName: 'test'
},
status: 404,
message: "email not found"
},
{
describe: 'should return error trying so signin a too long firstName',
body: {
firstName: 'kldsfjghsldkglsqkdjghqlkfjdsghldfksjghfdlskjgkldjfsdj',
lastName: 'test',
password: 'testhdksjdhfb',
email: 'test@aa.aa'
},
status: 400,
message: "invalid firstName"
},
];

for (var i in provider) {
it(provider[i].describe, function(done) {
request(url)
.post('/user/signin')
.send(provider[i].body)
.expect(provider[i].status)
.expect(function(res)
{
assert.equal(res.body.code, provider[i].status);
assert.equal(res.body.message, provider[i].message);
})
.end(done);
});
}
});
});

但在这种情况下,它只检查最后一个测试。

输出是

  Authentification
signin
✓ should return error trying to signin with empty body
✓ should return error trying to signin with no first name
✓ should return error trying to signin with no last name
✓ should return error trying so signin with no password
✓ should return error trying so signin with no email
✓ should return error trying so signin a too long firstName


6 passing (71ms)

但是如果最后一个测试失败,所有其他测试都会失败。如果其中一个测试错误,则测试通过。

可能是异步问题,不知道怎么解决

最佳答案

将您的 for 循环更改为如下所示:

function makeTest(p) {
it(p.describe, function(done) {
request(url)
.post('/user/signin')
.send(p.body)
.expect(p.status)
.expect(function(res) {
assert.equal(res.body.code, p.status);
assert.equal(res.body.message, p.message);
})
.end(done);
});
}

for (var i in provider) {
makeTest(provider[i]);
}

您的代码存在的问题是您实际上只测试了数组中的最后一个元素。 (是的,即使您看到了不同的测试名称。)您传递给 it 的匿名函数将在将来的某个时刻执行,当 Mocha 到达它时。到那时你的循环将完成执行并且 i 的值将是循环赋予它的最后一个值。对于您提供给 it 的第一个参数,这不是问题,因为该参数会立即求值。这就是为什么测试名称没问题,但测试本身只是测试数组中最后一个元素的多个实例。

上面的代码通过将 provider[i] 传递给 makeTest 解决了这个问题。当 makeTest 中的匿名函数引用创建时使用的 p 时,它具有 makeTest 时使用的值打电话。

关于node.js - 循环 Mocha 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28648022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com