简体   繁体   中英

How to write code for unit testing header in mocha and chai for Node.js express framework application?

I am writing test cases to check with all headers received are correctly spelled or not suppose there are multiple field names I want to make test cases for all of them that they all are spelled correctly written as in the array of objects. So how I code in mocha and chai

You can try something like this as it is not clear what your use case is, these are some of the tests I have used before .to.deep.equalInAnyOrder will throw an error if the spellings do not match. You have to create test data I am not entirely sure on what you are looking for but the second might be more appropriate.

it('200', async () => {

      nock('https://test.ca')

        .get('/test')

        .reply(200, {

          data: 'test',

        });

      const result = await rest.get('test', 'test');

      assert.equal(result.statusCode, 200);

      assert.deepEqual(result.body, { data: 'test' });

      assert.isTrue(nock.isDone());

    });

or

you can create an object that has all the results in my case s10

it('Get student details', () => {

      return request(app)

        .get('/api/v3/student/10')

        .expect(httpStatus.OK)

        .then(async (res) => {

          expect(res.body).to.be.an('object');

          expect(res.body.career).to.be.an('array');

          expect(res.body.career).to.deep.equalInAnyOrder(s10.career);

        });

    });

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM