HSG
HSG

Reputation: 45

MOQ WCF Service

I need to MOQ wcfClientService while calling the SomeMethod().

Class ABC : IABC
{

    internal WcfClientService  wcfClientService = new WcfClientService();

    public void SomeMethod(object pqr)
    {
        using(wcfClientService)
        {
            wcfClientService.Save(some parameters) 
        }
    }
}

Upvotes: 0

Views: 144

Answers (1)

Amol
Amol

Reputation: 4037

With the current implementation, you cannot isolate the "ABC" class as it is tightly coupled with wcfClientService. I would strongly suggest things mentioned below:

  1. Extract an interface IClientService. This makes your "ABC" class depend on an abstraction instead of a concrete implementation. It will help in short term to isolate "ABC" better for unit testing. In long term, your "ABC" class would not have to be changed if a "RestfulClientService" was to be used.
  2. Consider introducing a Dependency Injection framework. Anything like a Spring.Net, Unity or Autofac should serve the purpose. Ideally, your production code should never instantiate a dependency. Let the framework take care of it.
  3. Now, register and resolve a mock implementation of the interface using the DI framework and start unit testing the "ABC" class.

Upvotes: 1

Related Questions