The best way to mock external web services when testing a web application in development
I am currently working on an application that depends on many external web services. Some of them are authorize.net and are paid.
When testing (hand testing) things other than integrating with these web services, I replace these web service dependencies with fake versions that don't really do anything. The way I am doing it at the moment is using the following line of code in the structural map registry class:
For<IChargifyService>().Use<MockChargifyService>(); //uncomment this line to use a mock chargify service
I have similar registry lines for other fake services. I comment on them when deploying so that real services are used in production. Real and fake service implementations are present in the assembly Infrastructure
.
The problem with this approach is that I have to remember to uncomment the lines before deploying. I know there is a way to do this using Structure Xml Config, but I was wondering if there is a better way to do this. Would creating an assembly be a Mock Infrastructure
good idea?
a source to share
There are several ways I can think of:
1) You can create a separate assembly as you said that contains all your mock implementations. You must also include a registry in this assembly, which sets dummy implementations as defaults. The registry in your main assembly will have to scan in order to load your prefab assembly when needed - something like:
Scan(x =>
{
x.TheCallingAssembly();
x.AssembliesFromApplicationBaseDirectory();
x.LookForRegistries();
});
2) Another option is to create a profile for your mocks:
Profile("Test", x =>
{
x.For<IChargifyService>().Use<MockChargifyService>();
// etc.
});
Then somewhere in your application you would call:
ObjectFactory.Profile = "Test";
based on some environmental conditions that indicate you are in test mode.
a source to share