最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jestjs - Meaningful unit tests for TypeORM queries - Stack Overflow

programmeradmin0浏览0评论

Using TypeORM, I created some rather important queries with key functionality. Example: Only entities that match the anisationId may be returned.

// Typescript TypeORM code
const norm = await this.normRepository.findOne({
  where: {
    id: normId,
    chapter: {
      framework: {
        anisationId: this.userServiceanisationId,
      },
    },
  },
});

// Unit test
expect(normRepository.findOne).toHaveBeenCalledWith({
  // The same query that I inserted in the code above
});

To me this test does not really test any query logic.

Coming from a .NET EF Core background it would look like this:

// C# EF Core code
var norm = await _dbContext.Norms.FirstAsync(n => 
  n.Id == normId 
  && n.Chapter.Framework.OrganisationId == _userServiceanisationId);

// Unit test
_dbContext.Setup(s => s.Norms).ReturnsDbSet( new List<Norm> {
  // An example norm object, with connected chapter and framework
});

var result = await service.example();

Assert.NotNull(result) // Assert something that the method does.

In the EF Core unit test, some data is prepared on which the logic of the query actually runs.

Is there a way for TypeORM to test the query itself a bit better? What I foresee happening is that I update the query mistakenly, and then just copy paste the same query into the unit test.

发布评论

评论列表(0)

  1. 暂无评论