简体   繁体   English

我怎样才能端到端地测试这个 .net 核心控制台应用程序?

[英]How can I end to end test this .net core console application?

I have a .Net Core 2.0 console application.我有一个 .Net Core 2.0 控制台应用程序。 The main() method is like this: main() 方法是这样的:

public static async Task Main()
{
    // create  host builder
    var hostBuilder = new HostBuilder();
    var myApp = new MyApplication();

    // Build host
    var host = hostBuilder
        .ConfigureHostConfiguration(myApp.ConfigureHostConfiguration)
        .ConfigureServices(myApp.ConfigureServices)
        .ConfigureLogging(myApp.ConfigureLogging)
        .Build();

    // Initialise
    await myApp.InitialiseAppAsync(host);

    // Run host
    await host.RunAsync(CancellationToken.None);
}

The MyApplication class sets up the application configuration in ConfigureHostConfiguration(), it then configures up the dependencies in ConfigureServices() some of which register a Message Handlers to handle specific Messages types from an Azure Service Bus. MyApplication 类在 ConfigureHostConfiguration() 中设置应用程序配置,然后在 ConfigureServices() 中配置依赖项,其中一些注册消息处理程序以处理来自 Azure 服务总线的特定消息类型。 The application needs to do some initialisation from within InitialiseAppAsync().应用程序需要在 InitialiseAppAsync() 中进行一些初始化。 When host.RunAsync() is called, the a console application is run indefinitely and the Message Handler receives execution as soon as a message is available on the Azure Service Bus.调用 host.RunAsync() 时,控制台应用程序将无限期运行,并且一旦 Azure 服务总线上的消息可用,消息处理程序就会接收执行。 This is all great and working fine.这一切都很棒,而且工作正常。

What I'd like to do is create a new project under the same solution which contains some end to end tests (using XUnit).我想做的是在包含一些端到端测试(使用 XUnit)的相同解决方案下创建一个新项目。 I'd like to be able to override some of the dependencies (with test mocks, using NSubstitute), leaving the other dependencies as they are configured in the service.我希望能够覆盖一些依赖项(使用测试模拟,使用 NSubstitute),保留其他依赖项,因为它们在服务中配置。

I'm guessing I'd need to create my own HostBuilder in my test, so I'll need to be able to setup the mocks before the host.RunAsync() call is made within the test.我猜我需要在我的测试中创建我自己的 HostBuilder,所以我需要能够在测试中调用 host.RunAsync() 之前设置模拟。

Does anyone know how I can do this?有谁知道我怎么能做到这一点? Or what is the best practice for doing this?或者这样做的最佳做法是什么?

Ultimately, what I'm trying to do is be able to override some (but not all) of my real dependencies in my Console Application with mocks, so I can do some end to end tests.最终,我想要做的是能够用模拟覆盖我的控制台应用程序中我的一些(但不是全部)真正的依赖项,所以我可以做一些端到端的测试。

Thanks in advance提前致谢

There are multiple ways to achieve this.有多种方法可以实现这一点。 You can set up the environment variable "environment" when you start up your application.您可以在启动应用程序时设置环境变量“environment”。 Then you would need to run your application passing it like this:然后你需要运行你的应用程序,像这样传递它:

dotnet "MyApplication.dll" --environment end2endTests

Then you will be able to find the value you passed as the environment at IHostEnvironment instance which is injectable.然后,您将能够在可IHostEnvironment实例中找到作为环境传递的值。 This is how your DI registrations would look like:这是您的 DI 注册的样子:

services.AddScoped<IFoo>(provider =>
{
   var env = provider.GetRequiredService<IHostEnvironment>();

   if (env.EnvironmentName == "end2endTests")
   {
      return new TestFoo();
   }
   return new RealFoo();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何测试 .net 核心 web ZDB974238714CA8DE634A7CE1Dt083A 中的 CORS 策略是否可以访问前端? - How to test the CORS policy in .net core web API, if I dont have access to front end? Worldpay api 错误。 我怎样才能从他们那里收到令牌? ASP .NET 核心 MVC - Worldpay api error. How can I receive token from their end? ASP .NET Core MVC 如何在没有换行符的情况下将字符串打印到控制台? - How can I print a string to the console without a newline at the end? 如何将控制台应用程序中的光标设置到末尾? - How to set the cursor in a console application to the end? 我可以在 dot net core 控制台应用程序中使用 BackgroundService - Can I use BackgroundService in dot net core console application 如何在 WPF .NET 核心应用程序中启用控制台? - How do I enable the console in a WPF .NET Core application? 如何在 Application Insights 中显示超过 RabbitMQ 的端到端事务? - How can I show a end-to-end transaction over RabbitMQ in Application Insights? 如何在C#/ .Net中模拟最终进程树 - How can I emulate End Process Tree in C#/.Net 如何在 .NET 控制台应用程序中获取应用程序的路径? - How can I get the application's path in a .NET console application? 如何为.NET Core控制台应用程序创建exe? - How can I create an exe for .NET Core console applications?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM