技术开发 频道

VS Team Edition在其它单元中的测试

  如果使用Mstest.exe在Visual Studio意外运行测试,那么它将启动Cassini,并正常访问Web服务,如图14所示。


图14  MSTest在ASP.NET内部运行测试


  测试Web服务时,实际上是在本地进程的上下文中测试本地Web服务代理,再与远程Web服务通信。开发人员并没有测试Web服务的方法,或者它调用运行在宿主Web服务上下文中的方法。如果希望对运行在ASP.NET上下文中的测试进行编码,应该怎样做呢?可以在ASP.NET内部承载测试框架,并运行测试。这是当然可能的。为什么需要这样做?部分测试需要进行一些ASP.NET上下文方面的检查,例如会话。

  考虑一个简单的示例,Web服务使用辅助方法添加两个数字,然后在ASP.NET会话中存储结果。
 

public class Helper
{
  
public static void AddPlacingResultInSession(int x, int y)
  {
    
int ret = x + y;
    HttpContext.Current.Session[
"lastsum"] = ret;
  }
}

[WebService(Namespace
= "http://example.org/calcservice")]
[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]
public class CalcWebService : System.Web.Services.WebService
{
  [WebMethod]
  
public int Add2(int x, int y)
  {
    Helper.AddPlacingResultInSession(x, y);
    
return (int)Session["lastsum"];
  }
}

 

 

0
相关文章