【问题标题】:Unit test WCF method单元测试WCF方法
【发布时间】:2011-10-28 13:24:40
【问题描述】:

我创建了 WCF 服务并尝试测试其中一种方法。我右键单击 WCF 服务方法并选择创建单元测试。

它创建了一个新的测试项目并创建了一个单元测试。

我尝试运行测试项目,但我不确定UrlToTest 的值应该是多少?我已将 url 放入服务中。

[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("C:\\VS Projects\\NetBranch4\\" + 
    "MobileCheckCapture\\MobileCheckCapture", "/")]
// [UrlToTest("http://localhost:45651/")]
[UrlToTest("http://localhost/mobilecc/mobilecc.svc")]
public void AuthenticateUserTest()
{
    // TODO: Initialize to an appropriate value
    MobileCC target = new MobileCC(); 

    // TODO: Initialize to an appropriate value
    string authenticateRequest = string.Empty;

    // TODO: Initialize to an appropriate value
    string expected = string.Empty; 
    string actual;
    actual = target.AuthenticateUser(authenticateRequest);
    Assert.AreEqual(expected, actual);
    Assert.Inconclusive("Verify the correctness of this test method.");
}

【问题讨论】:

    标签: .net wcf unit-testing


    【解决方案1】:

    您最好手动滚动您自己的测试,而不是让 VS 为您构建一个。只需新建服务,就好像它是测试中的普通类并调用该函数,断言您期望返回的值。我所有的 WCF 服务都像普通类一样进行测试,现在实际上连接到服务并获得答案更多的是集成测试,因为连接和确保端点正确与测试服务的逻辑并不真正相关。

    ETA:我首先测试逻辑,因为很多时候连接问题、防火墙问题等可能需要时间来解决 WCF 服务,我保留最后测试。

    【讨论】:

      【解决方案2】:

      HostType、AspNetDevelopmentServerHost 和 UrlToTest 是用于 ASP.NET UnitTest 的参数,而不是 WCF。只需注释这些属性,设置输入参数和断言并运行测试。

      [TestMethod()]
      public void AuthenticateUserTest()
      {       
          MobileCC target = new MobileCC(); // TODO: Initialize to an appropriate value   
          string authenticateRequest = string.Empty; // TODO: Initialize to an appropriate value
          string expected = string.Empty; // TODO: Initialize to an appropriate value       string actual;
          actual = target.AuthenticateUser(authenticateRequest);
          Assert.AreEqual(expected, actual);
          Assert.Inconclusive("Verify the correctness of this test method.");
      } 
      

      希望这会有所帮助。

      【讨论】:

      • 谢谢它现在可以工作,但我已经在实际 = target.AuthenticateUser(authenticateRequest);它不会中断,让我调​​试 wcf 方法
      • 转到 VS 菜单中的测试,选择“调试 -> 在当前上下文中测试”。在 VS 的测试工具工具栏中也有运行测试的选项。
      • 我在调试菜单下没有看到在当前上下文中测试选项,我需要进入选项进行设置吗?谢谢
      • 您可以做两件事:首先,在 VS 菜单中,转到“查看 -> 工具栏”,然后从列表中选择“测试工具”(如果尚未选择)。执行此操作后,将显示一个新的工具栏菜单,其中包含一些看起来像向右箭头的绿色图标。其中一个有一个实心绿色箭头和白色矩形,中间有一个绿色圆圈。那就是那个。其次,在 VS 菜单中转到“测试 -> 调试”。在显示的列表中,您应该看到“在当前上下文中测试”选项。确保您选择了测试(字面意思是,您打开了测试文件并选择了测试名称)。
      【解决方案3】:

      要成功运行 web 服务的测试方法,您应该完全删除属性 [HostType("ASP.NET")]。此外,UrlToTest 应该只包含指向 Web 应用程序的 URL,而不是 SVC 文件的 URL。测试方法也只在某些特定情况下需要AspNetDevelopmentServer

      如果您将 SVC 托管在本地 IIS 上,则测试方法的代码将类似于:

      [TestMethod()]
      [UrlToTest("http://localhost/ServiceApp")]
      public void ServiceTest()
      {
          WcfService target = new WcfService();
          string arg = "test";
          Response actual = target.DoSmth(arg);
      
          Assert.IsTrue(actual != null);
      }
      

      【讨论】:

        猜你喜欢
        • 2010-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多