4

I am testing a web service client and need to isolate the call to the web service.

I have already been told that we can not use dependency injection. How can I isolate the webservice so that I can test the business logic that calls that service. Since I can not control the webservice I need to be sure of the value that the service will return.

I have been told that using Microsoft Fakes I can do this, but do not know how to define the fake.

A simplified version of the code that I am testing is:

var client = new Serviceprovider.ServiceProxies.PromoService.PromoServiceClient();
client.ClientCredentials.Windows.AllowedImpersonationLevel = client.Open();
Promo promoFromMaps = client.GetPromoById(exitingPm.NetworkId, true, true, true, exitingPm.MapsPromoId);

I want to control the values stored in promoFromMaps to be able to test the business logic that occurs after that.

Thanks for your time.

  • Have you tried this: http://code.msdn.microsoft.com/windowsdesktop/Unit-Test-a-having-404c2a18 – jparram Mar 24 '14 at 20:38
  • It looks like you'd need to shim the needed members of PromoServiceClient. Something along the lines of `ShimPromoServiceClient.GetPromoByIdInt32BooleanBooleanBooleanInt32 = () => new Promo()`, though you may want to shim something inside instead. I can't really tell what part you need isolated and what part you're testing. I'd also push back a bit, because even if this works, shims are naturally ugly code. – Magus Mar 24 '14 at 20:57

1 Answers1

0

It looks like you should read the following Microsoft documentation on the subject: https://msdn.microsoft.com/en-us/library/hh549175.aspx

What you need to do is right-click on the Serviceprovider reference and select Add Fake. This will create a folder in your test project called Fakes. After that the entire DLL is faked and you can use the faked PromoService through the name ShimPromoService. You can then set it up something like this:

ShimPromoService.PromoServiceClient = () => new Client(); //Probably return a mock or something

That way you can return a mock if you want to (using Moq, NSubstitute etc) from the PromoServiceClient method.

Maffelu
  • 2,018
  • 4
  • 24
  • 35