It is strongly recommended that development be based on test-driven
. Test-driven
development can effectively precipitate development results and lock potential problems as soon as code changes occur, thus significantly improving the robustness of the codes
On the basis of EggJS
, EggBornJS provides a convenient framework for the development based on test-driven
Module test-party
When a new project is created, the module
test-party
is automatically downloaded to the directory where the project is located. Contains a large number oftest-cases
andkitchen-sink
, and continues to add more content. It is not only convenient to quickly learn all aspects of knowledge points ofCabloyJS
, but also convenient to quickly start the actual business development
In this tutorial, the following tests are demonstrated. You can use this as an example to continuously expand test cases
and increase code coverage
as much as possible
- Login
- Create party
- Submit party (change status from
draft
tonormal
) - Read party
- Delete party
Unit Test
src/suite-vendor/test-party/modules/test-party/backend/test/controller/test.test.js
- 1const { app, mockUrl, mockInfo, assert } = require('egg-born-mock')(__dirname);
- 2
- 3describe('action: atom: party', () => {
- 4 it('[atom]', async () => {
- 5 app.mockSession({});
- 6
- 7 // atomClass info
- 8 const atomClassModule = mockInfo().relativeName;
- 9 const atomClassName = 'party';
- 10
- 11 // login as root
- 12 await app.httpRequest().post(mockUrl('/a/authsimple/passport/a-authsimple/authsimple')).send({
- 13 auth: 'root',
- 14 password: '123456',
- 15 });
- 16
- 17 // create
- 18 let result = await app.httpRequest().post(mockUrl('/a/base/atom/create')).send({
- 19 atomClass: { module: atomClassModule, atomClassName },
- 20 });
- 21 assert(result.body.code === 0);
- 22 const atomKey = result.body.data;
- 23
- 24 // submit
- 25 result = await app.httpRequest().post(mockUrl('/a/base/atom/submit')).send({
- 26 key: atomKey,
- 27 item: {
- 28 atomName: 'party demo',
- 29 personCount: 3,
- 30 },
- 31 });
- 32 assert(result.body.code === 0);
- 33
- 34 // read
- 35 result = await app.httpRequest().post(mockUrl('/a/base/atom/read')).send({
- 36 key: atomKey,
- 37 });
- 38 assert(result.body.code === 0);
- 39
- 40 // delete
- 41 result = await app.httpRequest().post(mockUrl('/a/base/atom/delete')).send({
- 42 key: atomKey,
- 43 });
- 44 assert(result.body.code === 0);
- 45
- 46 });
- 47});
Name | Description |
---|---|
mockUrl | used to construct a complete backend API route |
mockInfo | used to obtain basic information about the module currently under test. For example, the current module name is test-party , but in order to improve the flexibility of the code, it can be obtained by mockInfo().relativeName |
app.mockSession | used to mock session environment |
Run Test
- 1$ npm run test:backend
Code Coverage
- 1$ npm run cov:backend
API Route: version/test
Before the test cases are executed, the backend API route version/test
of each module will be executed automatically
At this stage, you can prepare some test data for testing (such as role
, user
, permission
, etc.)
Take the module test-party
as an example, add the following test data:
- Add user
demo001
- Add user’s login authentication information
- Append user
demo001
to roleregistered
src/suite-vendor/test-party/modules/test-party/backend/src/service/version.js
- 1async test() {
- 2 // create test users: demo001
- 3 const users = [
- 4 { userName: 'demo001', roleName: 'registered' },
- 5 ];
- 6 const userIds = [];
- 7 for (const user of users) {
- 8 // add user
- 9 userIds[user.userName] = await this.ctx.meta.user.add({
- 10 userName: user.userName,
- 11 realName: user.userName,
- 12 });
- 13 // add auth info for login
- 14 await this.ctx.performAction({
- 15 method: 'post',
- 16 url: '/a/authsimple/auth/add',
- 17 body: {
- 18 userId: userIds[user.userName],
- 19 password: '123456',
- 20 },
- 21 });
- 22 // role
- 23 const role = await this.ctx.meta.role.get({ roleName: user.roleName });
- 24 // add user role
- 25 await this.ctx.meta.role.addUserRole({
- 26 userId: userIds[user.userName],
- 27 roleId: role.id,
- 28 });
- 29 }
- 30}
app.meta.isTest, app.meta.isLocal
The corresponding codes can be loaded in the specified environment through app.meta.isTest
or app.meta.isLocal
For example, the resources in the module test-party
can only take effect in the test environment
and development environment
, but not in the production environment
. You can operate as follows:
src/suite-vendor/test-party/modules/test-party/backend/src/models.js
- 1const party = require('./model/party.js');
- 2const partyType = require('./model/partyType.js');
- 3const partyPublic = require('./model/partyPublic.js');
- 4
- 5module.exports = app => {
- 6 const models = {
- 7 };
- 8 if (app.meta.isTest || app.meta.isLocal) {
- 9 Object.assign(models, {
- 10 party,
- 11 partyType,
- 12 partyPublic,
- 13 });
- 14 }
- 15 return models;
- 16};
Comments: