What would be the best way to create unit tests for testing the parameters I send to my WCF service?
I have a project where I have a repository class that speaks to my WCF service. It looks something like this:
public class MyRepository : IMyRepository
{
public Customer GetCustomer(int customerId)
{
var client = new MyWCFServiceClient();
MyWCFServiceCustomer customerWCF = client.GetCustomer(customerId);
Customer customer = ConvertCustomer(customerWCF);
return customer;
}
//Convert a customer object recieved from the WCF service to a customer of
//the type used in this project.
private Customer ConvertCustomer(MyWCFServiceCustomer customerWCF)
{
Customer customer = new Customer();
customer.Id = customerWCF.Id;
customer.Name = customerWCF.Name;
return customer;
}
}
(This is obviously simplified)
Now I would like to write unit tests to check that the parameters I send to my service from the repository is correct. In the example above it would be kind of pointless since I only send the customerId just as it is passed in, but in my real code there are more parameters and some more logic in the repository class.
The problem is that the generated service client class (MyWCFServiceClient) doesn't have an interface and so I can't mock it in my tests (or am I wrong about that?).
Edit: I was wrong about this. There is an interface! See my answer below.
One solution would be to have a class that wraps the service client and that only re-sends the parameters and returns the result:
public class ClientProxy : IClientProxy
{
public MyWCFServiceCustomer GetCustomer(int customerId)
{
var client = new MyWCFServiceClient();
return client.GetCustomer(customerId);
}
}
public interface IClientProxy
{
MyWCFServiceCustomer GetCustomer(int customerId);
}
That way I could give that class an interface and thus mock it. But it seems tedious to write that "proxy" class and keep it updated, so I was hoping you have a better solution! :)