How to change the WCF service for the integration test

advertisements

I have a WPF program, using the MVVM architecture, that accesses a SQL Server through WCF. I have got it to the point where it needs to be integration tested, i.e. the program is working correctly, and unit tests have all passed.

I have found very little information on exactly how to do integration testing, and have never done it in the past. The problem I am facing is that all my View Models which need access to the WCF service have a parameter on their constructors for IDataService which is injected into the constructor by my View Model Locator.

Here is a sample of the service where CdaService refers to my development database:

public class DataService : CdaServiceManager.IDataService
{
    public void Select(Action<CdaServiceManager.CdaService.DatabaseTable> callback, CdaServiceManager.CdaService.DatabaseTable thisTable)
    {
        using (CdaService.Service1Client webService = new CdaService.Service1Client())
        {
            var item = webService.Select(thisTable);
            callback(item);
        }
    }
}

I have created an exact replica of my development environment on a seperate server, database and WCF service are exactly the same. During integration tests the database is cleared and reset to start with fresh data.

In my test, I have a different Service Reference called CdaService that points to the test server WCF. When I call my view mdoel constructor, I obviously cannot send my development version of IDataService since it is pointing to the real server.

I see one option of creating my own implementation of IDataService for the test environment, but then any time the production environment service changes I will have to make sure to change the test environment service. Seems sloppy.

Is there a better way to do this. Ideally I would be able to send the DataService the WCF reference I would like it to use, but I can't seem to wrap my head around it. Any other suggestions or ideas would be appreciated.


Have you considered using a mocking library like Moq or FakeItEasy?

Alternatively if you have a test web service set up, you don't need 2 different service references, you just have to pass the endpoint to the client on construction. For example:

var client = new YourServiceClient("Binding_in_your_config_file", "http://testservice.svc");