如何使用 Neo4JClient 对方法进行单元测试
How to unit test methods using Neo4JClient
我一直致力于实现 .NET Core API,使用 Neo4J 作为数据存储,使用 Neo4JClient (https://github.com/Readify/Neo4jClient) 构建应用程序的数据层。一切进展顺利,但我不知道如何使用客户端以充分验证代码是否按预期执行的方式测试方法的策略。
使用 Neo4JClient 的示例方法:
private readonly IGraphClient _graphClient;
protected IGraphClient GraphClient => _graphClient;
public BaseRepository(GraphClient client)
{
_graphClient = client;
_graphClient.Connect();
}
public async Task<IList<TModel>> GetAllAsync()
{
var results = await GraphClient.Cypher.Match($"(e:{typeof(TModel).Name})")
.Return(e => e.As<TModel>())
.ResultsAsync;
return results.ToList();
}
是否存在像这样在 GraphClient
上运行的模拟和单元测试方法的现有文档?我在有关该主题的 Google 次搜索中找不到任何内容。
Fluent API 似乎是个好主意,直到有人想嘲笑它们。
但是,至少 Neo4JClient 图形客户端是基于接口的。
您可以这样做(您需要将构造函数参数更改为 IGraphClient
而不是 GraphClient
。
public class BaseRepositoryTests
{
private readonly BaseRepository<Model> subject;
private readonly Mock<ICypherFluentQuery> mockCypher;
private readonly Mock<ICypherFluentQuery> mockMatch;
private readonly Mock<IGraphClient> mockGraphClient;
public BaseRepositoryTests()
{
mockMatch = new Mock<ICypherFluentQuery>();
mockCypher = new Mock<ICypherFluentQuery>();
mockCypher
.Setup(x => x.Match(It.IsAny<string[]>()))
.Returns(mockMatch.Object);
mockGraphClient = new Mock<IGraphClient>();
mockGraphClient
.Setup(x => x.Cypher)
.Returns(mockCypher.Object);
subject = new BaseRepository<Model>(mockGraphClient.Object);
}
[Fact]
public async Task CanGetAll()
{
IEnumerable<Model> mockReturnsResult = new List<Model> { new Model() };
var mockReturn = new Mock<ICypherFluentQuery<Model>>();
mockMatch
.Setup(x => x.Return(It.IsAny<Expression<Func<ICypherResultItem, Model>>>()))
.Returns(mockReturn.Object);
mockReturn
.Setup(x => x.ResultsAsync)
.Returns(Task.FromResult(mockReturnsResult));
var result = await subject.GetAllAsync();
Assert.Single(result);
}
public class Model { }
}
我一直致力于实现 .NET Core API,使用 Neo4J 作为数据存储,使用 Neo4JClient (https://github.com/Readify/Neo4jClient) 构建应用程序的数据层。一切进展顺利,但我不知道如何使用客户端以充分验证代码是否按预期执行的方式测试方法的策略。
使用 Neo4JClient 的示例方法:
private readonly IGraphClient _graphClient;
protected IGraphClient GraphClient => _graphClient;
public BaseRepository(GraphClient client)
{
_graphClient = client;
_graphClient.Connect();
}
public async Task<IList<TModel>> GetAllAsync()
{
var results = await GraphClient.Cypher.Match($"(e:{typeof(TModel).Name})")
.Return(e => e.As<TModel>())
.ResultsAsync;
return results.ToList();
}
是否存在像这样在 GraphClient
上运行的模拟和单元测试方法的现有文档?我在有关该主题的 Google 次搜索中找不到任何内容。
Fluent API 似乎是个好主意,直到有人想嘲笑它们。
但是,至少 Neo4JClient 图形客户端是基于接口的。
您可以这样做(您需要将构造函数参数更改为 IGraphClient
而不是 GraphClient
。
public class BaseRepositoryTests
{
private readonly BaseRepository<Model> subject;
private readonly Mock<ICypherFluentQuery> mockCypher;
private readonly Mock<ICypherFluentQuery> mockMatch;
private readonly Mock<IGraphClient> mockGraphClient;
public BaseRepositoryTests()
{
mockMatch = new Mock<ICypherFluentQuery>();
mockCypher = new Mock<ICypherFluentQuery>();
mockCypher
.Setup(x => x.Match(It.IsAny<string[]>()))
.Returns(mockMatch.Object);
mockGraphClient = new Mock<IGraphClient>();
mockGraphClient
.Setup(x => x.Cypher)
.Returns(mockCypher.Object);
subject = new BaseRepository<Model>(mockGraphClient.Object);
}
[Fact]
public async Task CanGetAll()
{
IEnumerable<Model> mockReturnsResult = new List<Model> { new Model() };
var mockReturn = new Mock<ICypherFluentQuery<Model>>();
mockMatch
.Setup(x => x.Return(It.IsAny<Expression<Func<ICypherResultItem, Model>>>()))
.Returns(mockReturn.Object);
mockReturn
.Setup(x => x.ResultsAsync)
.Returns(Task.FromResult(mockReturnsResult));
var result = await subject.GetAllAsync();
Assert.Single(result);
}
public class Model { }
}