문제

다음 코드에서 종속성을 분리하는 데 어려움을 겪고 있습니다.

    public static SiteConnector ConnectToSite(String Logon, String Password)
    {

        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(_URI);
        ConfigureRequest(Logon, Password, webRequest);
        HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
        Cookie ReposonseCookie;
        //this looks for a cookie and spitsout a value based on response
        int value = ProcessResponse(webResponse,out  ReposonseCookie);

        return new SiteConnector(ReposonseCookie, value);

    }

기본적으로 외부 웹사이트에 대한 요청에 의존하지 않고 단위 테스트를 수행하고 싶습니다.

이 문제를 해결하는 가장 좋은 방법은 무엇입니까?

도움이 되었습니까?

해결책

그 클래스가 내 머릿속에서 어떻게 보이는지 잘 모르겠지만, 언제든지 테스트 가능한 자신만의 클래스를 둘러쌀 수 있습니다.

public class WebRequestWrapper
{
   internal WebRequestWrapper() {..}

   public WebRequestWrapper(WebRequest req)
   {
      _innerRequest = req;
   }


   public virtual string Url
   {
      return _innerReq.Url;
   }

   //repeat, make all necessary members virtual
}

그런 다음 RhinoMocks를 사용하여 이 클래스의 PartialMock을 만들 수 있습니다.IT는 모든 가상 속성을 재정의합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top