2015-01-27 2 views
1

다른 로케일에서 다르게 작동하는 일부 코드를 유닛 테스트하고 있습니다. 나는 가짜 HttpContext을 만들었지 만, 그것에 대해 locale을 설정해야하고 할 수 없었습니다. 여기에 내가 가짜 HttpContext을 만드는 오전 방법입니다위조 된 HttpContext에 위조 된 로케일 설정

public static HttpContext FakeHttpContext(string requestUrl) 
    { 
     var httpRequest = new HttpRequest("", requestUrl, ""); 
     var stringWriter = new StringWriter(); 
     var httpResponce = new HttpResponse(stringWriter); 
     var httpContext = new HttpContext(httpRequest, httpResponce); 

     var sessionContainer = new HttpSessionStateContainer("id", new SessionStateItemCollection(), 
               new HttpStaticObjectsCollection(), 10, true, 
               HttpCookieMode.AutoDetect, 
               SessionStateMode.InProc, false); 

     httpContext.Items["AspSession"] = typeof(HttpSessionState).GetConstructor(
            BindingFlags.NonPublic | BindingFlags.Instance, 
            null, CallingConventions.Standard, 
            new[] { typeof(HttpSessionStateContainer) }, 
            null) 
          .Invoke(new object[] { sessionContainer }); 

     return httpContext; 
    } 

답변

0

을 마지막으로 해결 : 이미 여기에 설명되어 있습니다

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); 
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US"); 

:

/// <summary> 
    /// Fake HTTPCONTEXT generator 
    /// </summary> 
    /// <param name="extention">Http context extention</param> 
    /// <param name="domain">Http context domain</param> 
    /// <param name="locale">Http context locale</param> 
    /// <returns>Fake Http Context</returns> 
    private static HttpContext FakeHttpContext(string extention, string domain, string locale = "en-US,en;q=0.8") 
    { 
     HttpWorkerRequest httpWorkerRequest = new SimpleWorkerRequestHelper(false, domain, extention, locale); 
     return new HttpContext(httpWorkerRequest); 
    } 

코드를 SimpleWorkerRequestHelper을 위해 :

public class SimpleWorkerRequestHelper : SimpleWorkerRequest 
{ 
    /// <summary> 
    /// Whether the request is secure 
    /// </summary> 
    private string _domain; 

    /// <summary> 
    /// Whether the request is secure 
    /// </summary> 
    private string _locale; 

    /// <summary> 
    /// Initializes a new instance of the <see cref="SimpleWorkerRequestHelper" /> class. 
    /// </summary> 
    /// <param name="isSecure">Whether the helper request should be secure</param> 
    public SimpleWorkerRequestHelper(bool isSecure, string domain = "", string extention = "/", string locale = "") 
     : base(extention, AppDomain.CurrentDomain.BaseDirectory, string.Empty, string.Empty, new StringWriter()) 
    { 
     _domain = domain; 
     _locale = locale; 
    } 

    /// <devdoc> 
    /// <para>[To be supplied.]</para> 
    /// </devdoc> 
    public override String GetRemoteAddress() 
    { 
     if (string.IsNullOrEmpty(this._domain)) 
     { 
      return base.GetRemoteAddress(); 
     } 
     else 
     { 
      return this._domain; 
     } 
    } 

    /// <devdoc> 
    /// <para>[To be supplied.]</para> 
    /// </devdoc> 
    public override String GetLocalAddress() 
    { 
     if (string.IsNullOrEmpty(this._domain)) 
     { 
      return base.GetLocalAddress(); 
     } 
     else 
     { 
      return this._domain; 
     } 
    } 

    /// <summary> 
    /// Overriding "GetKnownRequestHeader" in order to force "SimpleWorkerRequest" to return the fake value for locale needed for unit testing. 
    /// </summary> 
    /// <param name="index">Index associated with HeaderAcceptLanguage in lower level library</param> 
    /// <returns>The language or the value from base dll</returns> 
    public override string GetKnownRequestHeader(int index) 
    { 
     if (index == HttpWorkerRequest.HeaderAcceptLanguage && !string.IsNullOrEmpty(_locale)) 
     { 
      return _locale; 
     } 
     else 
     { 
      return base.GetKnownRequestHeader(index); 
     } 
    } 

} 
문화는 Thread.CurrentThread.CurrentCulture 속성을 사용하여 변경할 수 있습니다
0

백은 내가 더 잘 단위 테스트에 일부 서비스를 개방했다있다. HttpContextBase 및 HttpContextWrapper를 사용하여이 작업을 수행 할 수있었습니다. 이것들을 전혀 보지 않았습니까? 나는 그들이 나를 많이 도왔다는 것을 기억하는 것 같다.

여기 적어도 기사에 대해 이야기하고 있습니다. C# unit testing API 2 call

관련 문제