如何使用Windows服务的API?

发布于 2024-10-19 13:16:49 字数 947 浏览 5 评论 0原文

我有一个大型 Windows 服务应用程序。它在有时间限制的基础上执行操作。有时我需要能够独立于应用程序的其余部分使用它的某些功能。目前,我已经有了一组“单元测试”,它们调用各种来源并执行所需的功能。我的问题是这些不是单元测试,它们是我们公开 API 的方式。如果我们运行项目中的所有单元测试,我们将损坏一些生产数据。

我的问题是如何在不进行单元测试的情况下访问应用程序的某些功能?我在想也许在它的顶部有一个解释器之类的东西,你可以在其中调用功能的各个部分,但我不太确定从哪里开始。

我们代码中的单元测试的一个例子是:

[TestMethod]
public void TransferFunds()
{
    int accountNumberTo = 123456;
    int accountNumberFrom = 654321;
    var accountFrom = Store.GetAccount(accountNumberFrom);
    var accountTo = Store.GetAccount(accountNumberTo);
    double amountToTransfer = 1000;
    DateTime transactionDate = new DateTime(2010,01,01);
    Store.TransferFunds(accountFrom, AccountTo, amountToTransfer, transactionDate);
    var client = BankAccountService.Client();
    client.Contribute(accountNumberTo, amountToTransfer, transactionDate);
    client.Contribute(accountNumberFrom, amountToTransfer, transactionDate);
}

我们如何将其移出单元测试,但仍然能够运行这样的代码?

I've got a big windows service application. It performs actions on a time bound basis. Sometimes I need to be able to use some of it's functionality in isolation from the rest of the application. Currently I've got a battery of 'unit tests' which call into various sources and perform the desired functionality. My problem is these are not unit tests, they are the way we're exposing the API. If we run all the unit tests in the project, we'll be damaging some of our production data.

My question is how do I go about accessing some of the functionality of the application without unit testing? I was thinking of perhaps something like an interpreter over the top of it where you can call various parts of the functionality, but am not really that sure where to start.

An example of a unit test in our code will be:

[TestMethod]
public void TransferFunds()
{
    int accountNumberTo = 123456;
    int accountNumberFrom = 654321;
    var accountFrom = Store.GetAccount(accountNumberFrom);
    var accountTo = Store.GetAccount(accountNumberTo);
    double amountToTransfer = 1000;
    DateTime transactionDate = new DateTime(2010,01,01);
    Store.TransferFunds(accountFrom, AccountTo, amountToTransfer, transactionDate);
    var client = BankAccountService.Client();
    client.Contribute(accountNumberTo, amountToTransfer, transactionDate);
    client.Contribute(accountNumberFrom, amountToTransfer, transactionDate);
}

How can we move this out of unit tests, but still have the ability to run code like this?

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

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

发布评论

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

评论(1

挽手叙旧 2024-10-26 13:16:49

你的设置听起来很危险。我会根据您的不同需求创建单独的控制台应用程序。我还建议您删除所有危及生产数据的单元测试。进行这种单元测试是非常糟糕的!

Your setup sounds very dangerous. I would create separate console applications for your different needs. I would also remove recommend that you remove all unittests that endangers your production data. Having that sort of unittests is just down-right bad!

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