单元测试貂
Unit Testing Marten
我正在整合 IdentityServer4 的实现,使用 PostgreSQL 作为数据库,使用 Marten 作为 ORM,使用 GraphQL 作为 API。到目前为止,它在运行时运行良好。但是,我也在尝试进行单元测试,并且 运行 遇到了问题。我有一个 IdentityServer4 的 IClientStore 接口的自定义实现,其中 FindClientByIdAsync 方法的实现如下所示:
public async Task<Client> FindClientByIdAsync(string clientId)
{
var client = await _documentSession.Query<dbe.Client>().FirstOrDefaultAsync(c => c.ClientId == clientId);
return _mapper.Map<Client>(client); // AutoMapper conversion call
}
这在运行时效果很好。但是,我有以下测试,我正在尝试实施以驱除此代码:
[Fact]
public async Task FindClientByIdReturnsClient()
{
var clients = new []
{
new dbe.Client
{
ClientId = "123"
}
}.AsQueryable();
var queryable = new MartenQueryable<dbe.Client>(clients.Provider);
// _documentSession is a Moq Mock
_documentSession.Setup(x => x.Query<dbe.Client>()).Returns(queryable);
var store = new ClientStore(_documentSession.Object, _mapper);
var result = await store.FindClientByIdAsync("123");
Assert.NotNull(result);
Assert.Equal("123", result.ClientId);
}
我在测试尝试执行 FindClientByIdAsync 方法时发生错误:
System.InvalidCastException : Unable to cast object of type 'System.Linq.EnumerableQuery`1[StaticSphere.Persona.Data.Entities.Client]' to type 'Marten.Linq.IMartenQueryable'.
如果熟悉 Marten 的人可以提供一些见解,那就太好了!我已经完成了我的 Google 时间,但还没有找到关于这个主题的任何具体内容。
引用 Marten 的创建者的话可能与此处相关 (context):
You can mock a bit of IDocumentSession (Load, Store, SaveChanges,
maybe query by compiled query), but you’re gonna be in a world of hurt
if you try to mock the Linq support.
因此,一种解决方案是进行集成测试,您可以从 official Marten's repository or here.
中找到一些代码
我正在整合 IdentityServer4 的实现,使用 PostgreSQL 作为数据库,使用 Marten 作为 ORM,使用 GraphQL 作为 API。到目前为止,它在运行时运行良好。但是,我也在尝试进行单元测试,并且 运行 遇到了问题。我有一个 IdentityServer4 的 IClientStore 接口的自定义实现,其中 FindClientByIdAsync 方法的实现如下所示:
public async Task<Client> FindClientByIdAsync(string clientId)
{
var client = await _documentSession.Query<dbe.Client>().FirstOrDefaultAsync(c => c.ClientId == clientId);
return _mapper.Map<Client>(client); // AutoMapper conversion call
}
这在运行时效果很好。但是,我有以下测试,我正在尝试实施以驱除此代码:
[Fact]
public async Task FindClientByIdReturnsClient()
{
var clients = new []
{
new dbe.Client
{
ClientId = "123"
}
}.AsQueryable();
var queryable = new MartenQueryable<dbe.Client>(clients.Provider);
// _documentSession is a Moq Mock
_documentSession.Setup(x => x.Query<dbe.Client>()).Returns(queryable);
var store = new ClientStore(_documentSession.Object, _mapper);
var result = await store.FindClientByIdAsync("123");
Assert.NotNull(result);
Assert.Equal("123", result.ClientId);
}
我在测试尝试执行 FindClientByIdAsync 方法时发生错误:
System.InvalidCastException : Unable to cast object of type 'System.Linq.EnumerableQuery`1[StaticSphere.Persona.Data.Entities.Client]' to type 'Marten.Linq.IMartenQueryable'.
如果熟悉 Marten 的人可以提供一些见解,那就太好了!我已经完成了我的 Google 时间,但还没有找到关于这个主题的任何具体内容。
引用 Marten 的创建者的话可能与此处相关 (context):
You can mock a bit of IDocumentSession (Load, Store, SaveChanges, maybe query by compiled query), but you’re gonna be in a world of hurt if you try to mock the Linq support.
因此,一种解决方案是进行集成测试,您可以从 official Marten's repository or here.
中找到一些代码