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 code
On the basis of EggJS
, CabloyJS provides a convenient framework for the development based on test-driven
Module test-party
The module test-party
contains a large number of test-cases
and kitchen-sink
, and continues to add more content. It is not only convenient to quickly learn all aspects of knowledge points of CabloyJS
, 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
toformal
) - 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 data: {
- 14 auth: 'root',
- 15 password: '123456',
- 16 },
- 17 });
- 18
- 19 // create
- 20 let result = await app.httpRequest().post(mockUrl('/a/base/atom/create')).send({
- 21 atomClass: { module: atomClassModule, atomClassName },
- 22 });
- 23 assert(result.body.code === 0);
- 24 const keyDraft = result.body.data;
- 25
- 26 // submit
- 27 result = await app.httpRequest().post(mockUrl('/a/base/atom/writeSubmit')).send({
- 28 key: keyDraft,
- 29 item: {
- 30 atomName: 'party demo',
- 31 personCount: 3,
- 32 },
- 33 });
- 34 assert(result.body.code === 0);
- 35 const keyFormal = result.body.data.formal.key;
- 36
- 37 // read
- 38 result = await app.httpRequest().post(mockUrl('/a/base/atom/read')).send({
- 39 key: keyFormal,
- 40 });
- 41 assert(result.body.code === 0);
- 42
- 43 // delete
- 44 result = await app.httpRequest().post(mockUrl('/a/base/atom/delete')).send({
- 45 key: keyFormal,
- 46 });
- 47 assert(result.body.code === 0);
- 48
- 49 });
- 50});
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
Comments: