Creating a Mock object for a WCF service client class

Feb 27, 2009 at 9:47 PM

I am new to Unity DI container and I am looking for a way to test my presenter/controller, which uses WCF service to get data from the server, classes without needing to use the real WCF service client classes. All the calls to the service are asynchronous (I am supporting both Silverlight & WPF). The problem I have is that I can't easily create a different implementation off of the service's interface 'coz it is different from the proxy client class. Any suggestions will be greatly appreciated.

Feb 28, 2009 at 7:00 AM

Perhaps my project may provide some insight for your requirements.   I have some blog/webcast links on the home page. The "Unity, WCF and Configuration" webcast shows how, using my UnityContrib project, you can easily setup your WCF service so that it is resolved via Unity.  As you'll see in the webcast you can easily switch between implementation for your service contract with a simple update to the <Unity> section in the web services Web.Config file.

The only thing was it didn't fall into my vision of having only "1" configuration file to manage for an entire enterprise solution (which I have achieved and plugged into this project). So I added functionality (proof of concept) where I can control the class that is used by the WCF service from within the main Websites Web.Config file (see referenced link for more details).

Below I show a unit test that exercises the WCF Service.  The Presentation Tier has it's own BLL and DAL.  The DAL has a "Web Service Reference" and raises an event when data arrives, it is bubbled through the BLL and handled as shown below:

        public void MyTestInitialize()
            // Subscribe to baseclass MockBootStrapper event
            Boot.OnConfigureContainer += OnConfigureContainer;

            // Initialize base for unit test use
            InitializeBase(this, new TestEventArgs
                SolutionName = @"sdms\SDMS.Silverlight\SDMS.Silverlight.Web"

        void OnConfigureContainer(object sender, TestEventArgs e)
                .RegisterType<IEmployeeProviderBLL, EmployeeProviderBLL>()
                .RegisterType<IEmployeeProviderDAL, EmployeeProviderDAL>();

        private ObservableCollection<Employee_Data> employeeList;

        public void CanRetrieveListFromService()
            IEmployeeProviderBLL bll = Container.Resolve<IEmployeeProviderBLL>();
            bll.OnEmployeeEvent += EmployeeEventHandler;
            // Call async event to get employee list

            // allow process time to set employeeList below

            Assert.IsTrue(employeeList.Count > 0);

        void EmployeeEventHandler(object sender, EmployeeEventArgs e)
            employeeList = e.EmployeeList;


Mar 4, 2009 at 4:00 PM

Thanks for the reply. I did look at your project and learned a few things from there. However, the solution you showed in your webcast is a little different from what I wanted to do. Some of the presenters in my presentation layer is using service reference to get to the data. To test my presenters, I wanted to create a mock object for the client proxy, which should not use the service at all. 
I kind of found a way to do it but I don't think it is the ultimate solution. What I did was, I refactored an interface off of the generated ServiceClient class. Then, implemented that interface for my mock object. Please, let me know if you guys have better way of testing presenter classes without using the real service it is using.

Thank you.

Mar 4, 2009 at 4:29 PM
[ericpizon] Please, let me know if you guys have better way of testing presenter classes without using the real service it is using.

Sounds like you have a good solution for your environment - similiar to what we were doing.   Our problem was the amount of configuration work it took to deploy an application through development, staging and production processes (we have over 30 modules in one application, each with many views/presenters) throw an extra curve ball when we started using VSTS for automated unit testing.  

Our solution was to start moving data access out of our presenters into a Data Access Layer and wedging a Business Logic Layer between the presenter and data layer.   Our presenters can reuse these services via constructor injection to populate the model (wasn't practical to reuse Presenters across modules).  Since our UIs are databound to the model our Unit Test can be very effective.   Note: Our compiler directives, i.e., Debug, Release, etc, determines the configuration to used for each deployment environment.

Mar 4, 2009 at 4:37 PM
Edited Mar 4, 2009 at 4:39 PM
[ericpizon]  I wanted to create a mock object for the client proxy, which should not use the service at all. 

Towards the end of the following webcast I show how I can select the WCF provider to use from the Website's Web.Config File.  Perhaps you could do something similiar (using Unity to resolve IServiceProvider) in your Presenter?  Your app.config for your application and unit test would specify different IServiceProvider implementation.  

Which is the Unity, WCF and Configuration link on the home page.