Moq: unit testing a method relying on HttpContext

后端 未结 6 1012
情话喂你
情话喂你 2020-11-29 03:53

Consider a method in a .NET assembly:

public static string GetSecurityContextUserName()
{             
 //extract the username from request              
 st         


        
6条回答
  •  温柔的废话
    2020-11-29 04:20

    This is not really related in using Moq for unit testing of what you need.

    Generally we at work have a layered architecture, where the code on the presentation layer is really just for arranging things for being displayed on the UI. This kind of code is not covered with unit tests. All the rest of the logic resides on the business layer, which doesn't have to have any dependency on the presentation layer (i.e. UI specific references such as the HttpContext) since the UI may also be a WinForms application and not necessarily a web application.

    In this way you can avoid to mess around with Mock frameworks, trying to simulate HttpRequests etc...although often it may still be necessary.

提交回复
热议问题