通过单元测试中的依赖注入来调用控制器 [英] Invoking controllers with dependency injection from unit tests

查看:81
本文介绍了通过单元测试中的依赖注入来调用控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在尝试在下面的文章中关注:

https://www.asp.net/web-api/overview/testing-and-debugging/unit-testing-with-aspnet-web-api

这是有关如何为WebAPI项目设置单元测试的演练.

我说到了要创建一些模拟数据并将其传递到控制器的构造函数中的地步.

就我而言,这不起作用.

我对本文提供的方法存在的问题是,我的控制器构造函数采用了依赖于依赖项注入的服务接口.例如,我的ReportCardController看起来像这样:

公共类ReportCardController:ApiController
{
私有只读IPromComService _promComService;
私有只读IProjectService _projectService;
私有只读IProjectCriticalityDataService _projectCriticalityDataService;

公共ReportCardController(
IPromComService promComService,
IProjectService projectService,
IProjectCriticalityDataService projectCriticalityDataService)
{
_promComService = promComService;
_projectService = projectService;
_projectCriticalityDataService = projectCriticalityDataService;
}

在代码中的任何地方都不能手动创建这些服务,并将它们传递到控制器的构造函数中,如果尝试的话,这样做并不容易.

因此,尽管我可以重载构造函数,以使其接受一些模拟数据(如本文中的示例所示)代替服务,但它需要服务才能正常运行.

在我的应用程序中,控制器是自动(自动)创建的,服务也是自动创建和注入的.我通过浏览器发出的GET或POST请求启动该过程,例如:

http://localhost:60209/api/ReportCard/?projectId = 166

有谁知道从单元测试内部发送这样的请求的方法吗?还是无论如何从单元测试中调用依赖于依赖注入的控制器?

谢谢.

Hello,

I'm trying to following along in the article below:

https://www.asp.net/web-api/overview/testing-and-debugging/unit-testing-with-aspnet-web-api

This is a walkthrough for how to setup unit tests for a WebAPI project.

I get to the point where it says to create some mock data and pass it into the controller's constructor.

This doesn't work in my case.

The problem I have with the approach this article gives is that my controller constructors take service interfaces that rely on dependency injection. For example, my ReportCardController looks like this:

public class ReportCardController : ApiController
{
private readonly IPromComService _promComService;
private readonly IProjectService _projectService;
private readonly IProjectCriticalityDataService _projectCriticalityDataService;

public ReportCardController(
IPromComService promComService,
IProjectService projectService,
IProjectCriticalityDataService projectCriticalityDataService)
{
_promComService = promComService;
_projectService = projectService;
_projectCriticalityDataService = projectCriticalityDataService;
}

No where in the code do I manually create these services and pass them into the controller's constructor, and it would not be easy to do so if I tried.

So while I could overload the constructor such that it takes some mock data (like the examples in the article suggest) instead of services, it needs the services in order to function properly.

In my application, the controllers are automatically (automagically) created and the services are also automat(g)ically created and injected. I initiate the process by a GET or POST request made from the browser, something like:

http://localhost:60209/api/ReportCard/?projectId=166

Does anyone know of a way to send off a request like this from inside the unit test? Or just anyway to invoke controllers that rely on dependency injection from within unit tests?

Thanks.

推荐答案

gib898,

Hi gib898,

感谢您的信息.根据您的描述,您的问题与单元测试更相关.我们会将您的话题移至单元测试"论坛以获得专用支持.

Thank you for your post. Based on your description, your question is more related to Unit Testing. We will move your thread to Unit Testing forum for dedicated support.

最好的问候,
王丽

Best Regards,
Li Wang


这篇关于通过单元测试中的依赖注入来调用控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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