测试循环摩卡 [英] Test in loop mocha
问题描述
我正在尝试在Mocha中使用数据提供程序来编写更少的代码
I am trying to use data provider in mocha to write less code
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);
});
}
});
});
但是在这种情况下,它仅检查上一次测试.
But in this case it only check the last test.
输出为
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)
但是,如果最后一次测试失败,则其他所有测试都会失败.如果其他测试之一不正确,则测试通过.
But if the last test fail, all others test fail. and if one of the other test is wrong, the test pass.
可能存在一个异步问题,但我不知道如何解决
There is maybe an asynchronious problem, but I don't know how to solve it
推荐答案
将您的for
循环更改为以下内容:
Change your for
loop to something like this:
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
的第一个参数,这不是问题,因为该参数会立即求值.这就是为什么测试名称可以,但测试本身只是测试数组中最后一个元素的多个实例的原因.
The problem with the code you have is that you are actually only testing the last element in your array. (Yes, even though you see different test names.) The anonymous function you pass to it
will execute at some point in the future, when Mocha gets to it. By that time your loop will have finished executing and the value of i
will be the last value that the loop gave to it. It is not a problem for the first argument you give to it
because that argument is evaluated right away. This is why the test names are okay but the tests themselves are just multiple instances of testing the last element in your array.
上面的代码通过将provider[i]
传递给makeTest
解决了该问题.当makeTest
中的匿名函数引用创建时使用的p
时,它具有调用makeTest
时使用的值.
The code above solves the issue by passing provider[i]
to makeTest
. When the anonymous function in makeTest
then refers to the p
that was used when it was created, it has the value that was used when makeTest
was called.
这篇关于测试循环摩卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!