Best way to mock out external web services while testing a web application in development
Currently I am working on an application which depends on a lot of external web services. A few of them are authorize.net and chargify.
When testing(manual testing) things other than the integration with these web services, I replace these web service dependencies with fake versions of them which don't really do anything. The way I am doing it as of now, is by using the following line of code in the structure map registry class:
For<IChargifyService>().Use<MockChargifyService>(); //uncomment this line to use a mock chargify service
I have similar lines in the registry for other fake services. I comment them out while deploying so that the real services are used in production. The real and fake service implementations are present in the Infrastructure
开发者_开发技巧 assembly.
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 a Mock Infrastructure
assembly be a good idea?
There are a couple ways I can think of:
1) You can create a separate assembly, as you suggested, that contains all of your mock implementations. You would also include a Registry in that assembly which sets the mock implementations as the defaults. The Registry in your main assembly would have to do a Scan that to optionally load your mock assembly - 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 condition that indicates you are in test mode.
Take a look at Soap UI (Getting Started).
精彩评论