在开发中测试 Web 应用程序时模拟外部 Web 服务的最佳方法

发布于 2024-08-31 23:59:06 字数 546 浏览 5 评论 0原文

目前我正在开发一个依赖于很多外部网络服务的应用程序。其中一些是 authorize.netchargify

当测试(手动测试)除了与这些 Web 服务集成之外的其他内容时,我将这些 Web 服务依赖项替换为它们的假版本,这些版本实际上并没有做任何事情。我现在的做法是在结构映射注册表类中使用以下代码行:

    For<IChargifyService>().Use<MockChargifyService>(); //uncomment this line to use a mock chargify service

我在注册表中也有类似的其他伪服务行。我在部署时将它们注释掉,以便在生产中使用真正的服务。真实和虚假的服务实现存在于基础设施程序集中。

这种方法的问题是我必须记住在部署之前取消注释这些行。我知道有一种方法可以使用 Structure Xml Config 来做到这一点,但我想知道是否有更好的方法来做到这一点。创建模拟基础设施程序集是个好主意吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饭团 2024-09-07 23:59:06

我可以想到几种方法:

1)您可以按照您的建议创建一个单独的程序集,其中包含所有模拟实现。您还可以在该程序集中包含一个注册表,它将模拟实现设置为默认值。主程序集中的注册表必须进行扫描,以选择性地加载模拟程序集 - 类似于:

Scan(x =>
{
    x.TheCallingAssembly();
    x.AssembliesFromApplicationBaseDirectory();
    x.LookForRegistries();
});

2)另一种选择是为您的模拟创建配置文件:

Profile("Test", x =>
{
    x.For<IChargifyService>().Use<MockChargifyService>();
    // etc.
});

然后在您的应用程序中的某个位置调用:

ObjectFactory.Profile = "Test";

基于某些环境条件这表明您处于测试模式。

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.

似狗非友 2024-09-07 23:59:06

查看 Soap UI (开始使用)。

Take a look at Soap UI (Getting Started).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文