Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

A journey of a thousand miles begins with single step

Continuously updated…

Portal series:

  • Egg Introduction Basics Egg introduction to project creation
  • Egg Project Directory and controller
  • Egg Basic Egg unit tests

Unit testing

At work, after the Controller of a module is completed, corresponding test files need to be written to conduct unit tests, so that problems in logic can be found quickly and solved as soon as possible. Unit testing is divided into synchronous unit testing and asynchronous unit testing.

Synchronous unit tests

In the previous section, we wrote the product.js file. In this article, we write synchronous unit tests for the index method in product.js

Create a product.test.js file in test/app/ Controller to write unit tests for the product.js file

The following code

'use strict';

const { app } = require('egg-mock/bootstrap');

describe('test/app/controller/product.tets.js'.() = > {
  it('product index'.() = > {
    return app.httpRequest()
      .get('/product') // This is the route of index method
      .expect(200)
      .expect('product page'); // This is the same as the index method
  });
});
Copy the code

The first argument to the describe method is the path to the file and the second argument is an arrow function

The it method takes the first parameter to write a unit test for that method, the second parameter is an arrow function, the content of get is the route to the method, and expect is the return status code 200 and the value of ctx.body

After writing, we run YARN test on the console

Asynchronous unit testing

Before conducting asynchronous unit tests, we need to go to product.js and write a new asynchronous method after the index method

  • In the app/controller/product. The end of the index in js, add the following code

    async getPrice() {
      const { ctx } = this;
      await new Promise(resolve= > {
        setTimeout(() = > {
          resolve(ctx.body = 'The price of this product is $50');
        }, 5000);
      });
    }
    Copy the code
  • Configure the routing address of the above method in app/router.js

    router.get('/getPrice', controller.product.getPrice);
    Copy the code
  • Effect (5s after loading)

When getPrcie method write finished, return to the test/app/controller/product test. Js file. Write asynchronous unit tests

  • Added after the unit test method is synchronized

    it('product getPrice'.async() = > {await app.httpRequest()
        .get('/getPrice')
        .expect(200)
        .expect('The price of this product is $50');
    });	
    Copy the code
  • After writing, we run YARN test on the console

Write in the last

Follow me, more content continues to output

🌹 if you like, give it a thumbs up 👍🌹

🌹 feel harvest, can come to a wave of collection + attention, so as not to find you next time I 😁🌹

🌹 welcome everyone to leave a message exchange, criticism and correction, forwarding please indicate the source, thank you for your support! 🌹