3

How do I write a unit test to test the UriTemplates (such as [WebGet(Uritemlpate="{clientId}/returns")] in my WCF services?

For example, in Global.asax I have:

private void RegisterRoutes()
{
     RouteTable.Routes.Add(new ServiceRoute("clients", 
                new WebServiceHostFactory(), typeof(ClientService)));
}

In the ClientService I have a [WebGet(Uritemlpate="uri_1")]:

    [ServiceContract]
    public class ClientService 
    {
        [WebGet(UriTemplate = "uri_1")]
        public string GetCollection()
        {
            return "Method 1";
        }

        [WebGet(UriTemplate = "uri_2")]
        public string GetCollections()
        {
            return "Method 2";
        }
    }

I want to have a test that Asserts the url clients/uri_1 hits exactly method GetCollection of ClientService.

Dmitry Sadakov
  • 2,128
  • 3
  • 19
  • 34

1 Answers1

0

You can Unit Test the methods GetCollection and GetCollections by just calling them and checking the return is not null.

What you are actually want to do is an Integration Test. This requires the external dependency of a web server. Check out the difference between the types here What is Unit test, Integration Test, Smoke test, Regression Test?

It is worth while having both. We run Unit Tests when we commit code automatically, it is them auto-deployed to a integrations. domain, the Integration Tests are then run against this.

Community
  • 1
  • 1
iain
  • 1,693
  • 13
  • 19