为了账号安全,请及时绑定邮箱和手机立即绑定

在node js中编写用于搜索记录和排序记录的单元测试

在node js中编写用于搜索记录和排序记录的单元测试

神不在的星期二 2022-11-11 13:23:58
刚接触编写单元测试,只是想问一下下面的示例代码编写单元测试的例子是什么,比如什么时候使用断言?以及如何在下面的代码中测试过滤器?谢谢你。我在大学测试中使用摩卡咖啡#当前单元测试代码.....      it('load the hook', () => {        assert.ok(search, 'Load the hook');      });          it('searches for record and sort', async () => {            const search_key = "Rajesh"            const people = await People.find({          query: {            $limit: 25,            $skip: 0,            $sort: {              'people': 1,            },            $or: [{              name: {                $like: search_key,              },            }],          },        });      });
查看完整描述

1 回答

?
慕丝7291255

TA贡献1859条经验 获得超6个赞

正如我们所讨论的,我添加了用于按名称检查过滤器和排序的测试用例。对于排序,您可以使用 chai-sorting 包:npm install chai-sorted. 您需要根据下面的评论对代码进行一些调整。希望它会有所帮助


it('searches for record and sort', async () => {

    const search_key = "Rajesh";

    const limit = 25;

    //you will need to have `data` variable and link `search_key` and `limit` with it somehow

    const people = await search_data()(data);

    if (people) {

      //length must be less than or equal to `limit`

      expect(people.length).to.be.at.most(limit);


      // check sorting by name. you can change it as descendingBy if you need

      expect(people).to.be.ascendingBy("name");


      people.forEach(person => { 

        //checking filter for name

        expect(person.name).to.have.string(search_key);

      });

    }        

});


查看完整回答
反对 回复 2022-11-11
  • 1 回答
  • 0 关注
  • 120 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号