How to unit test whether a Core MVC controller action calls ControllerBase.Problem()

前端 未结 2 1447
你的背包
你的背包 2021-01-12 16:26

We have a controller that derives from ControllerBase with an action like this:

public async Task Get(int id)
{
  try
  {
           


        
2条回答
  •  天涯浪人
    2021-01-12 17:04

    In your tests, if you first create a ControllerContext, then ProblemDetails should be created as expected while executing controller code.

    ...
    MyController controller;
    
    [Setup]
    public void Setup()
    {
        controller = new MyController();
        controller.ControllerContext = new ControllerContext
        {
            HttpContext = new DefaultHttpContext
            {
                // add other mocks or fakes 
            }
        };
    }
    ...
    

提交回复
热议问题