简体   繁体   中英

How to change WCF service for integration test

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, ie 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");

Really late to reply to this, but what we ended up doing is using pre-processor directives to set the endpoint address, and then use that when creating the client. We had to do this because we were using an MVVM framework, so the DataService was not get instantiated with any constructor, it was using an IOC container. Hope it helps someone.

#if Test
  public EndpointAddress address = new EndpointAddress("http://localhost/xxx/Service1.svc");
#else
  public EndpointAddress address = new EndpointAddress("http://xxx.azurewebsites.net/Service1.svc");
#endif

  public void Select(Action<CdaServiceManager.CdaService.DatabaseTable> callback, CdaServiceManager.CdaService.DatabaseTable thisTable)
  {
     using (CdaService.Service1Client webService = new CdaService.Service1Client("WSHttpBinding_IService1", address))
     {
         var item = webService.Select(thisTable);
         callback(item);
     }
  }    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM