循环摩卡测试
Test in loop mocha
我正在尝试使用 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
时使用的值。
我正在尝试使用 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
时使用的值。