【发布时间】:2018-05-03 22:25:35
【问题描述】:
我创建了一个名为testLoginFailed的方法
let testloginFailed = (app, title, data) => {
it(title, function (done) {
request(app)
.post(apiEndPoints.auth.login)
.send(data)
.then((response) => {
response.statusCode.should.equal(401);
response.body.error.name.should.equal('Error');
response.body.error.message.should.equal('login failed');
response.body.error.code.should.equal('LOGIN_FAILED');
done();
})
.catch((error) => {
done(error);
});
});
};
这是我的描述块
describe('login negative Tests', () => {
before(function () {
let loginFailedTests = [
{
title: 'it should fail user login using mobile because of incorrect mobile',
data: {
username: '1223334444',
password: options.user.password
}
}, {
title: 'it should fail user login using mobile because of incorrect password',
data: {
username: options.user.mobileNumber,
password: options.user.password + '123'
}
}
];
});
loginFailedTests.forEach((test) => {
testloginFailed(app, test.title, test.data);
});
});
问题说明:
- 我想使用上述函数“testloginFailed”生成动态测试用例。
- 所以我用不同的测试数据集循环调用该方法testloginFailed。
- 数组 testloginFailed 正在 before 块中初始化,因为它需要使用选项在 glocal 范围内的一些数据。
问题:当我在上面的步骤 2 中尝试使用这个数组 testloginFailed 时,它说
loginFailedTests.forEach((test) => { ^ReferenceError: loginFailedTests 未定义
【问题讨论】:
-
尝试在 before 块外创建变量并在 before 块内设置它的值。
-
@pulkitsinghal 不起作用,因为在块之前声明 loginFailedTests 会给循环一个空数组,因此不会生成测试
标签: javascript mocha.js integration-testing chai supertest