iJa*_*ava 4 c# integration-testing .net-core
我正在尝试创建集成测试,但它也取决于我想要伪造的第三方服务。我有控制台应用程序 .Net Core 3.1。
我的意思是说:
var configuration = GetConfiguration();
var serviceProvider = GetServiceProvider(configuration);
var appService = serviceProvider.GetService<IConsumerManager>();
appService.StartConsuming(commandLineArguments);
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
就我而言,我想假冒给消费者的电话。我想知道除了创建 Fake 类并将其添加到 DI 之外,是否还有其他方法可以伪造对它的调用。例如:
collection.AddTransient<IConsumer, FakeConsumer>();
Run Code Online (Sandbox Code Playgroud)
也许我可以使用 FakeItEasy、NUnit 或任何其他库来伪造这个?
您可以使用您最喜欢的模拟框架创建模拟并将它们添加到您的服务集合中。
使用起订量的示例:
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);
Run Code Online (Sandbox Code Playgroud)
https://github.com/Moq/moq4/wiki/Quickstart
归档时间: |
|
查看次数: |
4276 次 |
最近记录: |