2017-01-19 15 views

答えて

1

あなたは目的の情報に

private class MockHttpContext : HttpContextBase { 
    private readonly MockRequest request; 
    public MockHttpContext(HttpCookieCollection cookies) { 
     this.request = new MockRequest(cookies); 
    } 

    public override HttpRequestBase Request { 
     get { 
      return request; 
     } 
    } 

    public class MockRequest : HttpRequestBase { 
     private readonly HttpCookieCollection cookies; 
     public MockRequest(HttpCookieCollection cookies) { 
      this.cookies = cookies; 
     } 

     public override HttpCookieCollection Cookies { 
      get { 
       return cookies; 
      } 
     } 
    } 

} 

をカプセル化し、この

//Arrange 
var cookies = new HttpCookieCollection(); 
cookies.Add(new HttpCookie("ASP.NET_SessionId")); 
cookies.Add(new HttpCookie("__RequestVerificationToken")); 

var context = new MockHttpContext(cookies); 
var Request = context.Request;//This is just for demonstrative purposes 

string[] myCookies = Request.Cookies.AllKeys; 

のように使用する偽のHttpContextを作成するか、またはあなたがあざけるフレームワークと同じことを行うことができます。

この例では、この例では、NSubstitute

var cookies = new HttpCookieCollection(); 
cookies.Add(new HttpCookie("ASP.NET_SessionId")); 
cookies.Add(new HttpCookie("__RequestVerificationToken")); 

var mockHttpContext = Substitute.For<HttpContextBase>(); 
mockHttpContext.Request.Cookies.Returns(cookies); 
を使用部品番号

var cookies = new HttpCookieCollection(); 
cookies.Add(new HttpCookie("ASP.NET_SessionId")); 
cookies.Add(new HttpCookie("__RequestVerificationToken")); 

var mockHttpContext = new Mock<HttpContextBase>(); 
mockHttpContext.Setup(m => m.Request.Cookies).Returns(cookies); 

を使用

関連する問題