调用 .DLL 的 NUnit 测试调用 WCF Web 服务 (.NET C#)
这在某种程度上与:
创建wcf Web服务实例时出现InvalidOperationException
我有一个调用 WCF Web 服务的 .NET C# 类库 (DLL)。似乎任何调用该 .DLL 的 .exe 现在都必须在其 .EXE.config 文件中包含 Web 服务配置。
例如,如果我编写 NUnit 测试,那么我是否必须使用 Web 服务参数更新 NUnit.exe.config。这似乎会变得非常难以管理。我在这里错过了一个要点吗?有什么捷径吗?如果我有几十个 WCF 服务和几十个 NUnit 测试,那么我的 NUnit.exe.config 似乎会很糟糕。
谢谢,
尼尔·沃尔特斯
This relates somewhat to:
InvalidOperationException while creating wcf web service instance
I have a .NET C# class library (DLL) that calls a WCF web service. It seems like any .exe that calls that .DLL must now have the web service configurations in its .EXE.config file.
So for example, if I write an NUnit test, then do I have to update the NUnit.exe.config with the web-service parms. This seems like it could get very difficult to manage. Am I missing a big point here? Any shortcuts? If I have dozens of WCF services, and dozens of NUnit tests, it seems like my NUnit.exe.config would be horrendous.
Thanks,
Neal Walters
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,你没有错过任何东西。在 .NET 中,配置默认留给托管应用程序 - 在 ASP.NET 中是 Web 运行时及其配置文件 - web.config。
在类库中,使用类库的托管应用程序负责通过其 app.config(编译后的 YourApp.exe.config)提供配置。
您可以做的是将 WCF 的 web.config 部分外部化为单独的 *.config 文件,然后再次引用 testapp.exe.config 中的文件:
不幸的是,您只能外部化配置部分,而不能外部化配置 部分组 (并且
是一个配置节组:-(因此您无法将其全局化)。Marc
No, you're not missing anything. In .NET, the configuration is by default left to the hosting app - in ASP.NET the web runtime and its config file - web.config.
In a class library, it's the hosting app that uses the class library which is responsible for providing the configuration through it's app.config (--> YourApp.exe.config after compilation).
On thing you could do is externalize the web.config sections for WCF into separate *.config files, and then reference those in your testapp.exe.config again:
Unfortunately, you can only externalize configuration sections but not configuration section groups (and
<system.serviceModel>
is a configuration section group :-( so you can't externalize it globally).Marc
出于单元测试的目的,在客户端,您应该模拟 Web 服务代理接口,以便客户端调用您的模拟,而无需准备好系统的其余部分。
在应用程序中,您可能会发现以编程方式显式地将参数(如地址和绑定)传递到 WCF 层更方便 - 由应用程序决定是否使用自己的配置。
编辑-扩展第一点。
WCF 的要点是采用一个接口并提供一个在其他地方运行的实现(通过 Channel 泛型类型)。在单元测试中,您希望能够用本地模拟替换 WCF 生成的接口实现。
将
Channel
的使用隔离到可以在测试工具中重写或替换的方法中,并返回接口类型的对象。然后在单元测试中,换入您自己的返回模拟的方法。执行完整的 WCF 连接以及单元测试中无法涵盖的少量手写代码是系统或集成测试阶段的一部分。
For unit test purposes, on the client side, you should be mocking the web service proxy interface, so that the client calls your mock without needing to have the rest of the system in place.
In the application, you may find it more convenient to pass parameters (like the address and binding) into the WCF layer explicitly programattically -- leaving it up to the application as to whether it uses its own configuration or not.
EDIT -- extending the first point.
The point of WCF is to take an interface and provide an implementation that is running somewhere else (through the
Channel
generic type). In unit testing, you want to be able to replace WCF-generated implementation to the interface with a local mock instead.Isolate the use of
Channel
into a method that you can override or replace in the test harness, and which returns an object of the interface type. Then in unit tests, swap in your own method that returns the mock instead.Exercising the full WCF connection -- and the small amount of hand-written code that can't be covered in unit testing -- is then part of the system or integration test stage.