我创建了一个名为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);
});
});
问题规则:
问题:当我尝试在上面的第2步中使用此阵列 testloginFailed 时,它说
loginFailedTests.forEach((test) => { ^
ReferenceError:未定义loginFailedTests
答案 0 :(得分:0)
来自文档 RUN CYCLE OVERVIEW。
<块引用>我们知道 describe()
及其回调将在 before
钩子之前执行。执行顺序为describe()
=> before()
=> it()
所以你应该把测试数据的初始化过程放在describe()
块中。
例如
const { expect } = require('chai');
const options = { user: { password: '123', mobileNumber: '321' } };
const app = {};
let testloginFailed = (app, title, data) => {
it(title, function () {
expect(1 + 1).to.be.eql(2);
});
};
describe('login negative Tests', () => {
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);
});
});
测试结果:
login negative Tests
✓ it should fail user login using mobile because of incorrect mobile
✓ it should fail user login using mobile because of incorrect password
2 passing (5ms)