5

I have created a WCF service and was trying to test one of the methods. I right clicked on the WCF service method and selected create unit test.

It created a new test project and created a unit test.

I tried to run test project but I am not sure what should be the UrlToTest value? I have put url to the service.

[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.");
}
casperOne
  • 73,706
  • 19
  • 184
  • 253
user228777
  • 3,082
  • 9
  • 33
  • 50

3 Answers3

4

You're better off hand-rolling your own tests rather than having VS build one for you. Just new up the service as if it's a normal class inside your test and call the function, assert against the value you expect back. All of my WCF services are tested like normal classes, now actually connecting to the service and getting answers back is more of an integration tests as connecting and ensuring endpoints are correct isn't really related to testing the logic of the service.

ETA: I test the logic first because a lot of the times connection problems, firewall issues, etc. can take time to solve with WCF services, and I reserve testing that last.

Mark W
  • 3,879
  • 2
  • 37
  • 53
3

The HostType, AspNetDevelopmentServerHost and UrlToTest are parameters used for ASP.NET UnitTest, not WCF. Just comment those attributes, set your input parameters and asserts and run the test.

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

Hope this helps.

Gabe Thorns
  • 1,426
  • 16
  • 20
  • Thank you it works now but I have put a break point on actual = target.AuthenticateUser(authenticateRequest); it doesn't break and let me debug wcf method – user228777 Oct 28 '11 at 14:44
  • Go to Test in VS Menu, select "Debug -> Test in Current Context". There are also options for running the Test in the Test Tools toolbars in VS. – Gabe Thorns Oct 28 '11 at 14:49
  • I don't see Test in current context option under debug menu, Do I need to go in options to set it up? thanks – user228777 Oct 28 '11 at 14:58
  • You can do two things: First, in VS menu, go to "View -> Toolbars" and from the list select "Test Tools" (if not already seleected). Once you do this a new toolbar menu will show up with some green icons that look like arrows pointing to the right. One of this has a filled green arrow and white rectagle with a green circle in the middle. That is the one. Second, in the VS menu go to "Test -> Debug". In the list that shows up you should see the option "Test in Current Context". Make sure you have your test selected (literally, you have your test file open and you selected the test name). – Gabe Thorns Oct 28 '11 at 15:06
0

To successfully run test-method for the web-service, you should remove attribute [HostType("ASP.NET")] at all. Also UrlToTest should contain an URL to the web-application only, not to SVC file. Also test-method needs AspNetDevelopmentServer in some specific cases only.

If you host you SVC on local IIS, the code of test-method will be similar to:

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

    Assert.IsTrue(actual != null);
}
Woonder
  • 291
  • 4
  • 4