2011-10-28 215 views
5

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

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

我试图运行测试项目,但我不知道UrlToTest值应该是多少?我已经把网址的服务。

[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."); 
} 

回答

3

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."); 
} 

希望这会有所帮助。

+0

谢谢你现在的工作,但我已经把一个破发点上的实际数量=目标.AuthenticateUser(authenticateRequest);它不会中断并让我调试wcf方法 – user228777

+0

转到VS菜单中的Test,选择“Debug - > Test in Current Context”。还有在VS的测试工具工具栏中运行测试的选项。 –

+0

我没有看到在调试菜单下的当前上下文中的测试选项,我是否需要进入选项进行设置?谢谢 – user228777

4

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

ETA:我首先测试逻辑,因为很多次连接问题,防火墙问题等都需要一些时间才能解决WCF服务,并且我保留最后的测试。

0

要成功运行Web服务的测试方法,您应该完全删除属性[HostType("ASP.NET")]。另外UrlToTest应该只包含一个到Web应用程序的URL,而不是SVC文件。在某些特定情况下,测试方法也需要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); 
}