我正在尝试创建集成测试,但它也依赖于我想要伪造的第三方服务。我有一个控制台应用程序 .Net Core 3.1。
我的意思是:
var configuration = GetConfiguration();
var serviceProvider = GetServiceProvider(configuration);
var appService = serviceProvider.GetService<IConsumerManager>();
appService.StartConsuming(commandLineArguments);
private static IConfiguration GetConfiguration()
=> new ConfigurationBuilder().AddJsonFile(ConfigurationFile, true, true).Build();
private static ServiceProvider GetServiceProvider(IConfiguration config)
{
IServiceCollection collection = new ServiceCollection();
collection.Configure<ConsumerConfig>(options => config.GetSection("consumerConfig").Bind(options));
collection.AddSingleton<IConsumerManager, ConsumerManager>();
collection.AddTransient<ISelfFlushingQueue, SelfFlushingQueue>();
collection.AddTransient<IConsumer, Consumer>();
collection.AddTransient<IConverter, Converter>();
collection.AddFactory<IConsumerWorker, ConsumerWorker>();
return collection.BuildServiceProvider();
}
在我的情况下,我想伪造对Consumer的调用。 我想知道是否有其他方法来伪造它的调用,而不仅仅是创建Fake类并将其添加到DI中。 例如:
collection.AddTransient<IConsumer, FakeConsumer>();
也许我可以使用FakeItEasy、NUnit或其他库来伪造它?