我正在尝试创建集成测试,但这也取决于我要伪造的第三方服务。我有控制台应用程序.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();
}
就我而言,我想伪造给消费者的电话。我想知道是否还有其他方法可以伪造对它的调用,而不仅仅是创建Fake类并将其添加到DI中。例如:
collection.AddTransient<IConsumer, FakeConsumer>();
也许我可以使用FakeItEasy,NUnit或任何其他lib来伪造此内容?
您可以使用自己喜欢的模拟框架创建模拟并将其添加到服务集合中。
使用最小起订量的示例:
var mock = new Mock<IConsumer>();
mock.Setup(foo => foo.DoSomething("ping")).Returns(true); // this line is just an example of mocking a method named DoSomething, you'll have to adapt it to the methods you want to mock
collection.AddTransient<IConsumer>(() => mock.Object);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句