我怎样才能端到端地测试这个 .net 核心控制台应用程序? [英] How can I end to end test this .net core console application?

查看:39
本文介绍了我怎样才能端到端地测试这个 .net 核心控制台应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .Net Core 2.0 控制台应用程序.main() 方法是这样的:

I have a .Net Core 2.0 console application. The main() method is like this:

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);
}

MyApplication 类在 ConfigureHostConfiguration() 中设置应用程序配置,然后在 ConfigureServices() 中配置依赖项,其中一些注册消息处理程序以处理来自 Azure 服务总线的特定消息类型.应用程序需要在 InitialiseAppAsync() 中进行一些初始化.调用 host.RunAsync() 时,控制台应用程序将无限期运行,并且一旦 Azure 服务总线上的消息可用,消息处理程序就会接收执行.这一切都很棒,而且工作正常.

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. The application needs to do some initialisation from within 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. This is all great and working fine.

我想做的是在包含一些端到端测试(使用 XUnit)的相同解决方案下创建一个新项目.我希望能够覆盖一些依赖项(使用测试模拟,使用 NSubstitute),保留其他依赖项,因为它们在服务中配置.

What I'd like to do is create a new project under the same solution which contains some end to end tests (using 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.

我猜我需要在测试中创建自己的 HostBuilder,所以我需要能够在测试中调用 host.RunAsync() 之前设置模拟.

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.

有谁知道我该怎么做?或者这样做的最佳做法是什么?

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.

提前致谢

推荐答案

有多种方法可以实现这一点.您可以在启动应用程序时设置环境变量environment".然后你需要像这样运行你的应用程序:

There are multiple ways to achieve this. You can set up the environment variable "environment" when you start up your application. Then you would need to run your application passing it like this:

dotnet "MyApplication.dll" --environment end2endTests

然后,您将能够在可注入的 IHostEnvironment 实例中找到作为环境传递的值.这是您的 DI 注册的样子:

Then you will be able to find the value you passed as the environment at IHostEnvironment instance which is injectable. This is how your DI registrations would look like:

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

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

这篇关于我怎样才能端到端地测试这个 .net 核心控制台应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆