如何使用 NSubstitute 框架验证接收到具有特殊类型的 AddSingleton
How to verify AddSingleton with special type is received using NSubstitute framework
我想模拟 IServiceCollection
以使用 Nsubstite
模拟库和 xUnit
.[=17= 检查是否使用特定接口和具体类型调用 AddSingleton
]
这是我的单元测试:
[Fact]
public checkIfServicesAddedTo_DI()
{
var iServiceCollectionMock = Substitute.For<IServiceCollection>();
var iConfiguration = Substitute.For<IConfiguration>();
MatchServicesManager servicesManager = new MatchServicesManager();
servicesManager.AddServices(iServiceCollectionMock, iConfiguration);
iServiceCollectionMock.Received(1).AddSingleton(typeof(IMatchManager) , typeof(MatchManager));
}
这是实现:
public class MatchServicesManager : IServicesManager
{
public void AddServices(IServiceCollection services, IConfiguration configuration)
{
services.AddSingleton<IMatchManager, MatchManager>();
}
}
我希望测试成功,但它因以下错误而失败:
NSubstitute.Exceptions.ReceivedCallsException : Expected to receive
exactly 1 call matching: Add(ServiceDescriptor) Actually received no
matching calls. Received 1 non - matching call(non - matching
arguments indicated with '*' characters): Add(*ServiceDescriptor *)
AddSingleton
是 IServiceCollection
上的扩展方法。这使得模拟或验证变得更加困难。
考虑使用接口的实际实现,然后在执行被测方法后验证预期行为。
例如
public void checkIfServicesAddedTo_DI() {
//Arrange
var services = new ServiceCollection();// Substitute.For<IServiceCollection>();
var configuration = Substitute.For<IConfiguration>();
MatchServicesManager servicesManager = new MatchServicesManager();
//Act
servicesManager.AddServices(services, configuration);
//Assert (using FluentAssertions)
services.Count.Should().Be(1);
services[0].ServiceType.Should().Be(typeof(IMatchManager));
services[0].ImplementationType.Should().Be(typeof(MatchManager));
}
我想模拟 IServiceCollection
以使用 Nsubstite
模拟库和 xUnit
.[=17= 检查是否使用特定接口和具体类型调用 AddSingleton
]
这是我的单元测试:
[Fact]
public checkIfServicesAddedTo_DI()
{
var iServiceCollectionMock = Substitute.For<IServiceCollection>();
var iConfiguration = Substitute.For<IConfiguration>();
MatchServicesManager servicesManager = new MatchServicesManager();
servicesManager.AddServices(iServiceCollectionMock, iConfiguration);
iServiceCollectionMock.Received(1).AddSingleton(typeof(IMatchManager) , typeof(MatchManager));
}
这是实现:
public class MatchServicesManager : IServicesManager
{
public void AddServices(IServiceCollection services, IConfiguration configuration)
{
services.AddSingleton<IMatchManager, MatchManager>();
}
}
我希望测试成功,但它因以下错误而失败:
NSubstitute.Exceptions.ReceivedCallsException : Expected to receive exactly 1 call matching: Add(ServiceDescriptor) Actually received no matching calls. Received 1 non - matching call(non - matching arguments indicated with '*' characters): Add(*ServiceDescriptor *)
AddSingleton
是 IServiceCollection
上的扩展方法。这使得模拟或验证变得更加困难。
考虑使用接口的实际实现,然后在执行被测方法后验证预期行为。
例如
public void checkIfServicesAddedTo_DI() {
//Arrange
var services = new ServiceCollection();// Substitute.For<IServiceCollection>();
var configuration = Substitute.For<IConfiguration>();
MatchServicesManager servicesManager = new MatchServicesManager();
//Act
servicesManager.AddServices(services, configuration);
//Assert (using FluentAssertions)
services.Count.Should().Be(1);
services[0].ServiceType.Should().Be(typeof(IMatchManager));
services[0].ImplementationType.Should().Be(typeof(MatchManager));
}