2

http://localhost:1070で実行される ASP.NET Web サービス (Microsoft.VisualStudio.WebHost.Server を使用) を開始する NUnit テスト クラスがあります。

私が抱えている問題は、NUnit テスト内で、localhost:1070 の ASP.NET Web サービスからアクセスできるセッション状態を作成したいということです。
私は次のことを行いました。セッション状態は NUnit テスト内で正常に作成できますが、Web サービスが呼び出されると失われます。

//Create a new HttpContext for NUnit Testing based on:
//http://blogs.imeta.co.uk/jallderidge/archive/2008/10/19/456.aspx
HttpContext.Current = new HttpContext(
    new HttpRequest("", "http://localhost:1070/", ""), new HttpResponse(
     new System.IO.StringWriter()));

//Create a new HttpContext.Current for NUnit Testing
System.Web.SessionState.SessionStateUtility.AddHttpSessionStateToContext(
 HttpContext.Current, new HttpSessionStateContainer("",
  new SessionStateItemCollection(),
  new HttpStaticObjectsCollection(), 20000, true,
  HttpCookieMode.UseCookies, SessionStateMode.Off, false));

HttpContext.Current.Session["UserName"] = "testUserName";
testwebService.testMethod(); 

ASP.NET Web サービスの Session["UserName"] の NUnit テストで作成されたセッション状態を取得できるようにしたいと考えています。

[WebMethod(EnableSession=true)]
public int testMethod()
{
    string user; 

    if(Session["UserName"] != null)
    {
       user = (string)Session["UserName"];

      //Do some processing of the user
      //user is validated against with value stored in database
      return 1;
    }
    else
      return 0;
}

web.config ファイルには、セッション状態構成用に次の構成があり、StateServer または SQLServer ではなく InProc を引き続き使用したいと考えています。

<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" cookieless="false" timeout="20"/> 
4

1 に答える 1

0

実際のHttpContextクラスを使用する代わりに、HttpContextBaseを使用してセッション、応答、要求などをモックすることができます。

于 2010-04-21T07:02:22.670 に答える