How do I make a unit test to test a method that checks request headers?

后端 未结 3 1089
北海茫月
北海茫月 2021-02-05 18:35

I am very, very new to unit testing and am trying to write a test for a pretty simple method:

public class myClass : RequireHttpsAttribute
{
    public override          


        
3条回答
  •  爱一瞬间的悲伤
    2021-02-05 19:22

    Yes, I'd use Moq and create a Mock. You'll need a series of mock objects to setup the fake request, most notably to specify a NameValueCollection of fake headers.

    var request = new Mock();
    request.SetupGet(c => c.Headers).Return(new NameValueCollection{ /* initialize values here */});
    request.SetupGet(c => c.IsSecureConnection).Return(/*specify true or false depending on your test */);
    
    var httpContext = new Mock();
    httpContext.SetupGet(c => c.Request).Return(request.Object);
    
    
    var filterContext = new Mock();
    filterContext.SetupGet(c => c.HttpContext).Return(httpContext.Object);
    
    var myclass = new myClass();
    myClass.OnAuthorization(filterContext.Object);
    

    (sorry if syntax or usage is slightly off; doing this from the top of my head)

    You may need to go in and mock any additional members on filterContext that HandleNonHttpsRequest invokes. I have two recommendations for going about this, as it can sometimes be a hassle if the method you are testing is doing lots of complex stuff on filterContext: 1) check visually and, if it's straight forward enough, mock all the invoked pieces 2) create the myClass.OnAuthorizationRequest, but don't implement any code yet other than the call to HandleNonHttpsRequest. Keep running the test and fixing missing/incorrectly mocked members until the test passes. Then implement your actual logic for OnAuthorizationRequest, testing and fixing (rinse repeat) until it passes.

提交回复
热议问题