Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

eggjs service 中使用了 egg-mysql 读取数据库,测试 service 时需不需要 mock?

$
0
0

Service 的主要任务就是对数据库进行 CRUD,测试时不知道怎么做才好:

  • 连接真实数据库的话,需要每次填充数据和清空数据
  • 使用 mock 的话,感觉没什么可测了,因为输入和输出都一样

比如 官方文档 Service 测试直接把 POST 请求 mock 掉了,类比数据库就是把 insert mock 掉了:

    it('should create success', async () => {
      // 不影响 CNode 的正常运行,我们可以将对 CNode 的调用按照接口约定模拟掉
      // app.mockHttpclient 方法可以便捷的对应用发起的 http 请求进行模拟
      app.mockHttpclient(`${ctx.service.topics.root}/topics`, 'POST', {
        data: {
          success: true,
          topic_id: '5433d5e4e737cbe96dcef312',
        },
      });

      const id = await ctx.service.topics.create({
        accesstoken: 'hello',
        title: 'title',
        content: 'content',
      });
      assert(id === '5433d5e4e737cbe96dcef312');
    });

assert(id === '5433d5e4e737cbe96dcef312');还有多大意义?


Viewing all articles
Browse latest Browse all 14821

Trending Articles