0

WCF Web サービスの単体テストを作成したいと考えています。サービスは HttpContext.Current を使用します。System.Web に Fake Assembly を追加し、次のようなコードを追加することで、すでに Fake を作成できました。

[Test]
public void TestMyService()
{
  using (ShimsContext.Create())
  {
    HttpRequest httpRequest = new HttpRequest("", "http://tempuri.org", "");
    HttpContext httpContext = new HttpContext(httpRequest, new HttpResponse(new StringWriter()));
    System.Web.Fakes.ShimHttpContext.CurrentGet = () => { return httpContext; };
    System.Web.Fakes.ShimHttpClientCertificate.AllInstances.IsPresentGet = (o) => { return true; };
  }
}

しかし、私のサービスには ClientCertificate も必要です。

if (!HttpContext.Current.Request.ClientCertificate.IsPresent) // <== Exception in unit test!
  throw new Exception("ClientCertificate is missing");
_clientCertificate = new X509Certificate2(HttpContext.Current.Request.ClientCertificate.Certificate);

マークされた行で、単体テストは NullReferenceException をスローします。

結果メッセージ: System.NullReferenceException : オブジェクト参照がオブジェクトのインスタンスに設定されていません。結果 StackTrace: System.Web.HttpClientCertificate..ctor(HttpContext コンテキスト) で System.Web.HttpRequest.CreateHttpClientCertificateWithAssert() で System.Web.HttpRequest.get_ClientCertificate() で (私のメソッド) で TestMyService()

単体テスト用に ClientCertificate を設定するにはどうすればよいですか? 適切なコンストラクターがないため、Shim を介して渡す HttpClientCertificate オブジェクトを作成する方法がわかりません。

4

1 に答える 1