Geeks With Blogs

News Locations of visitors to this page
Brian Genisio's House of Bilz

Previous posts: Part 0 of 4: Introduction

Shout it kick it on DotNetKicks.com

Testing the Service

Of the four posts, testing the service is by far the easiest.

One of the most beautiful things about the WCF framework is the way it was designed to be more testable than ASPX services.  When you design your WCF interface, you are mostly just designing an interface with the WCF ServiceContract attributes.  The WCF framework uses your interface to determine the actual contract and transport mechanism so you don't have to. 

This is the key to testing WCF services:  YOU DON'T HAVE TO WORRY ABOUT THE WCF FRAMEWORK.  This means that you can simply instantiate your service directly and start calling public methods on it.  You never need to worry about hitting the service through the transport layer.  You can focus on what is important: your code.

Recipe Box Service Interface

This is the interface that my WCF service publishes:

[ServiceContract]
public interface IRecipeBoxService
{
    [OperationContract] 
    RecipeData[] AllRecipes();

    [OperationContract]
    void SaveRecipe(RecipeData recipe);

    [OperationContract]
    void DeleteRecipe(int id);

    [OperationContract]
    IngredientData[] AllIngredients();

    [OperationContract]
    void SaveIngredient(IngredientData ingredient);

    [OperationContract]
    IngredientData[] FindIngredients(string nameIsLike);

    [OperationContract]  
    void DeleteIngredient(int id);
}

The Tests

The actual implementation is named RecipeBoxService, and that is the class I am interested in testing.   A test might look something like this:

[Test]
public void Test_Get_All_Recipes_Returns_SingleRecipe()
{
    PopulateDatabase(new Recipe("AAA", "BBB", "CCC"));

    var recipeBoxService = new RecipeBoxService();
    var recipes = recipeBoxService.AllRecipes();

    Assert.That(recipes.Length, Is.EqualTo(1));
    Assert.That(recipes[0].Title, Is.EqualTo("AAA"));
    Assert.That(recipes[0].Description, Is.EqualTo("BBB"));
    Assert.That(recipes[0].Author, Is.EqualTo("CCC"));
}

It is that simple.  YOU SHOULD NEVER NEED TO CREATE A SERVICE REFERENCE IN YOUR TEST PROJECT.  Forget about WCF and test your service logic directly.

Abstracting the back end

Note: I am using a database abstraction product called Castle Active Record in my service which gives me a lot of great features.  From a testing perspective, the most important feature is that you can swap out the actual database for a more testable back end.  Good unit tests (usually) do not rely on external databases, external services, file systems or any other environment-specific requirements.  In my approach, I am using an in-memory, temporal database that only lives as long as the test does.  Please see my post on mocking out the database with ActiveRecord or more information on this.

Next time

I will talk about how you test the client without connecting to the service. (Part 2 or 4)

Posted on Monday, November 24, 2008 9:38 PM | Back to top


Comments on this post: Testing WCF Service Apps (Part 1 of 4)

# re: Testing WCF Service Apps (Part 1 of 4)
Requesting Gravatar...
Hi,
Your articles are helpfull. I have a dll which developers have given me to test. According to them this dll which is a proxy of many services is going to be hosted in IIS. I wish to test not only the functionality but that when hosted the service works fine. I am new to WCF. Please suggest the best way to test functionality and that when the service is hosted in IIS.

Thanks,
Left by tester05 on Dec 23, 2008 1:22 PM

# re: Testing WCF Service Apps (Part 1 of 4)
Requesting Gravatar...
tester05: I am glad these articles are useful to you. I do want to point out that the articles are more suited for developers that are doing TDD and/or unit tests and not as useful for QA professionals.

That being said, it is my belief that auto-generated proxies do not need to be unit tested. The clients that use the service needs to be tested and the same is true for the service itself. BUT, testing auto-generated proxies and WCF configuration doesn't need unit tests from a developer's perspective.

From a QA perspective, the services do need to be tested in a production environment. In those cases, these articles are less useful.

Are you looking for automated testing methods? Or are you trying to figure out how to write an application for ad-hoc testing of the service? What is your perspective? Are you a QA tester, or a developer?

Sorry if my answer isn't very useful. I need to understand more of what you are trying to do before I can answer better.
Left by Brian Genisio on Dec 23, 2008 2:53 PM

# re: Testing WCF Service Apps (Part 1 of 4)
Requesting Gravatar...
We've been advised by a consultant that we DO need to test our services through WCF proxies. This contradicts your advice in this article.
One of the reasons highlighted is if the data contracts exposed by our services have problems with serialization, e.g. cycles in the object graph, types that can't be serialized.
Like tester05, I'm now trying to come up with a manageable way to write these tests, preferably without manually generating client proxies via a service reference. Any ideas how to create client proxies on the fly for the tests, e.g. reflection, codegen?
Left by Simon on Sep 08, 2009 12:43 PM

# re: Testing WCF Service Apps (Part 1 of 4)
Requesting Gravatar...
@Simon: Well, if you are testing the services via proxies, you are not unit testing... which is the focus of part 1-3 of this article. You are talking about integration tests at that point. I don't think I am saying "never test" this way, but don't go overboard when writing automated tests that test the service. There can be some really great reasons to write integration/functional tests, but any that rely on WCF endpoints will be fragile and slow.

That being said, you might look at this GREAT article about the makings of service proxies. He talks about a more dynamic proxy mechanism, which might be nice for what you are trying to do. It is a long read (and focuses on Silverlight), but the concepts play well and the read is really worth it. http://www.netfxharmonics.com/2008/11/Understanding-WCF-Services-in-Silverlight-2

I would, however, question why you feel like you need to test the services running under WCF. Doing so really violates one of the basic tenants of WCF -- They manage transports for you, so you don't have to.

If you want to test serializations to make sure they serialize properly, you can write unit tests using the DataContractSerializer directly. Same with deserialization. You might find that other concerns can still be pinpointed with something that more resembles a unit test.

Just to re-itterate: Automated tests that require an actual WCF endpoint are fragile. You will want to minimize the number tests you create this way, and try to make as many of your tests of the unit-test style.

Of course, this is my opinion... others may disagree and I welcome their comments :)
Left by Brian Genisio on Sep 08, 2009 1:01 PM

# re: Testing WCF Service Apps (Part 1 of 4)
Requesting Gravatar...
< href="http://www.bonus-paris.fr/">meilleurs bonus du casino virtuel
I have found one somewhat big problem with the implementation provided in that the stack trace doesn’t appear to be preserved from the server. I understand that this may actually be desirable depending on the amount of information that one desires to disclose to the client however I have found a hack that seems to be working for us in the meantime.......
Left by hthjyky on Apr 23, 2010 11:50 PM

# re: Testing WCF Service Apps (Part 1 of 4)
Requesting Gravatar...
< href="http://www.bonus-paris.fr/">meilleurs bonus du casino virtuel
I have found one somewhat big problem with the implementation provided in that the stack trace doesn’t appear to be preserved from the server. I understand that this may actually be desirable depending on the amount of information that one desires to disclose to the client however I have found a hack that seems to be working for us in the meantime.......////
Left by bfnfh on Apr 23, 2010 11:51 PM

Your comment:
 (will show your gravatar)
 


Copyright © Brian Genisio's House Of Bilz | Powered by: GeeksWithBlogs.net | Join free