我创建了一个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.");
}
答案 0 :(得分:4)
你最好亲自动手测试自己的测试,而不是让VS为你构建一个测试。刚刚启动服务,就好像它是测试中的普通类并调用函数一样,根据您期望的值进行断言。我的所有WCF服务都像普通类一样进行测试,现在实际上连接到服务并获得答案更多的是集成测试,因为连接和确保端点正确与测试服务逻辑无关。
ETA:我首先测试逻辑,因为很多时候连接问题,防火墙问题等都需要时间来解决WCF服务,我保留最后的测试。
答案 1 :(得分: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.");
}
希望这有帮助。
答案 2 :(得分:0)
要成功运行Web服务的测试方法,您应该删除属性[HostType("ASP.NET")]
。此外,UrlToTest
应仅包含Web应用程序的URL,而不应包含SVC文件。此外,测试方法仅在某些特定情况下需要AspNetDevelopmentServer
。
如果您在本地IIS上托管SVC,则test-method的代码类似于:
[TestMethod()]
[UrlToTest("http://localhost/ServiceApp")]
public void ServiceTest()
{
WcfService target = new WcfService();
string arg = "test";
Response actual = target.DoSmth(arg);
Assert.IsTrue(actual != null);
}