In my project I am using: SL5+ MVVM+ Prism + WCF + Rx + Moq + Silverlight Unit Testing Framework.
I am new to unit-testing and have recently started into DI, Patterns (MVVM) etc. Hence the following code has a lot of scope for improvement (please fell free to reject the whole approach I am taking if you think so).
To access my WCF services, I have created a factory class like below (again, it can be flawed, but please have a look):
namespace SomeSolution { public class ServiceClientFactory:IServiceClientFactory { public CourseServiceClient GetCourseServiceClient() { var client = new CourseServiceClient(); client.ChannelFactory.Faulted += (s, e) => client.Abort(); if(client.State== CommunicationState.Closed){client.InnerChannel.Open();} return client; } public ConfigServiceClient GetConfigServiceClient() { var client = new ConfigServiceClient(); client.ChannelFactory.Faulted += (s, e) => client.Abort(); if (client.State == CommunicationState.Closed) { client.InnerChannel.Open(); } return client; } public ContactServiceClient GetContactServiceClient() { var client = new ContactServiceClient(); client.ChannelFactory.Faulted += (s, e) => client.Abort(); if (client.State == CommunicationState.Closed) { client.InnerChannel.Open(); } return client; } } }
It implements a simple interface as below:
public interface IServiceClientFactory { CourseServiceClient GetCourseServiceClient(); ConfigServiceClient GetConfigServiceClient(); ContactServiceClient GetContactServiceClient(); }
In my VMs I am doing DI of the above class and using Rx to call WCF as below:
var client = _serviceClientFactory.GetContactServiceClient(); try { IObservable> observable = Observable.FromEvent(client, "GetContactByIdCompleted").Take(1); observable.Subscribe( e => { if (e.EventArgs.Error == null) { //some code here that needs to be unit-tested } }, ex => { _errorLogger.ProcessError(GetType().Name, MethodBase.GetCurrentMethod().Name, ErrorSource.Observable, "", -1, ex); } ); client.GetContactByIdAsync(contactid, UserInformation.SecurityToken); } catch (Exception ex) { _errorLogger.ProcessError(GetType().Name, MethodBase.GetCurrentMethod().Name, ErrorSource.Code, "", -1, ex); }
Now I want to build unit tests (yes, its not TDD). But I don't understand where to start. With Moq I can't mock the BlahServiceClient. Also no svcutil generated interface can help because async methods are not part of the auto-generated IBlahService interface. I may prefer to extend (through partial classes etc) any of the auto generated classes, but I would hate to opt for manually building all the code that svcutil can generate (frankly considering time and budget).
Can someone please help? Any pointer in the right direction would help me a lot.