I have created a method called 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);
});
});
};
and here is my describe block
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);
});
});
Problem Statment:
Issue: When I am trying to use this array testloginFailed in step 2 above, it says
loginFailedTests.forEach((test) => { ^
ReferenceError: loginFailedTests is not defined
From the doc RUN CYCLE OVERVIEW .
- When a test file is loaded, Mocha executes all of its suites and finds–but does not execute–any hooks and tests therein.
- Any “before all” hooks (for the root suite, this only happens once; see root hook plugins)
We know that the describe()
and its callback will be executed before the before
hook. The order of execution is describe()
=> before()
=> it()
So you should put the initialization process of test data in describe()
block.
Eg
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);
});
});
test result:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.