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

  1. Login
  2. Create party
  3. Submit party (change status from draft to formal)
  4. Read party
  5. Delete party

Unit Test

src/suite-vendor/test-party/modules/test-party/backend/test/controller/test.test.js

  1. 1const { app, mockUrl, mockInfo, assert } = require('egg-born-mock')(__dirname);
  2. 2
  3. 3describe('action: atom: party', () => {
  4. 4 it('[atom]', async () => {
  5. 5 app.mockSession({});
  6. 6
  7. 7 // atomClass info
  8. 8 const atomClassModule = mockInfo().relativeName;
  9. 9 const atomClassName = 'party';
  10. 10
  11. 11 // login as root
  12. 12 await app.httpRequest().post(mockUrl('/a/authsimple/passport/a-authsimple/authsimple')).send({
  13. 13 data: {
  14. 14 auth: 'root',
  15. 15 password: '123456',
  16. 16 },
  17. 17 });
  18. 18
  19. 19 // create
  20. 20 let result = await app.httpRequest().post(mockUrl('/a/base/atom/create')).send({
  21. 21 atomClass: { module: atomClassModule, atomClassName, atomClassIdParent: 0 },
  22. 22 });
  23. 23 assert(result.body.code === 0);
  24. 24 const keyDraft = result.body.data;
  25. 25
  26. 26 // submit
  27. 27 result = await app.httpRequest().post(mockUrl('/a/base/atom/writeSubmit')).send({
  28. 28 key: keyDraft,
  29. 29 item: {
  30. 30 atomName: 'party demo',
  31. 31 personCount: 3,
  32. 32 },
  33. 33 });
  34. 34 assert(result.body.code === 0);
  35. 35 const keyFormal = result.body.data.formal.key;
  36. 36
  37. 37 // read
  38. 38 result = await app.httpRequest().post(mockUrl('/a/base/atom/read')).send({
  39. 39 key: keyFormal,
  40. 40 });
  41. 41 assert(result.body.code === 0);
  42. 42
  43. 43 // delete
  44. 44 result = await app.httpRequest().post(mockUrl('/a/base/atom/delete')).send({
  45. 45 key: keyFormal,
  46. 46 });
  47. 47 assert(result.body.code === 0);
  48. 48
  49. 49 });
  50. 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. 1$ npm run test:backend

Code Coverage

  1. 1$ npm run cov:backend