0

テストにはRhinoMocksを使用しています。静的をリダイレクトするのは得意ではありません。Molesの後継(編集:FakesツールはVS2012でしか利用できないと思いますか?それは臭い)またはTypeMockのような別のライブラリを使用することを検討しましたが、使用しないことを好みます。

HttpRequest オブジェクトを受け取るサードパーティ ライブラリがあります。私の最初の刺し傷は、次のものを使用することでした:

public void GetSamlResponseFromHttpPost(out XmlElement samlResponse, 
  out string relayState, HttpContextBase httpContext = null)
 {
  var wrapper = httpContext ?? new HttpContextWrapper(HttpContext.Current); 
  // signature of the next line cannot be changed
  ServiceProvider.ReceiveSAMLResponseByHTTPPost(
      wrapper.ApplicationInstance.Context.Request, out samlResponse, out relayState);

テストに行くまでは、すべて問題ないように見えました。ここでの本当の問題は、スタブアウトする必要があるということですwrapper.ApplicationInstance.Context.Request。これは、古い学校の「ASP.NETはテストが好きではない」という苦痛のホスト全体につながります.

dynamicC# でマジックを使用して静的メソッドをリダイレクトできると聞いたことがあります。ただし、HttpContext などでこれを行う例は見つかりません。これは可能ですか?

4

1 に答える 1

0

理想的な解決策ではありませんが、これをテストするための私の解決策は、反射を使用してボンネットの下のオブジェクトを変更することでした:

httpRequest = new HttpRequest("default.aspx", "http://test.com", null);
var collection = httpRequest.Form;

// inject a value into the Form directly
var propInfo = collection.GetType().GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
propInfo.SetValue(collection, false, new object[] { });
collection["theFormField"] = val;
propInfo.SetValue(collection, true, new object[] { });

var appInstance = new HttpApplication();
var w = new StringWriter();
httpResponse = new HttpResponse(w);
httpContext = new HttpContext(httpRequest, httpResponse);

// set the http context on the app instance to a new value
var contextField = appInstance.GetType().GetField("_context", BindingFlags.Instance | BindingFlags.NonPublic);
contextField.SetValue(appInstance, httpContext);

Context.Stub(ctx => ctx.ApplicationInstance).Return(appInstance);

ここでの私の目標はwrapper.ApplicationInstance.Context.Request、要求されたときにフォーム フィールドの値を返すことでした。回り道だったかもしれませんが、うまくいきます。このコードはテスト コードにしか存在しないので、満足しています。

于 2012-06-28T23:11:17.770 に答える