c# - 如何对我的 WCF 客户端类进行单元测试?

标签 c# .net visual-studio-2010 wcf .net-4.0

我有一个与 WCF 服务交互的类库,并使用 app.config 文件进行配置。我想对这个类进行单元测试,但是当我运行单元测试时,我得到:

Could not find default endpoint element that references contract 'FooService' in the service model client configuaration section. This might be because no configuration file was found for your application or because no end point element matching this contract could be found in the client element

根据 this answer ,我需要 app.config 文件驻留在我的单元测试项目中,这确实解决了问题。但我真的不想在每次更改时都将我的 app.config 文件复制到单元测试中。

我想我可以添加一个指向 app.config 的链接,除了我使用的是 SlowCheetah。处理 app.config 转换,所以我的 app.config 是在编译时生成的。

我可以做些什么来让它工作,还是我只需要放弃我的 app.config 并在代码中处理配置?

最佳答案

您可以在测试中模拟 WCF 服务。在对类进行单元测试时,可以注入(inject)不使用 WCF 服务的服务版本。这样,当测试运行时,他们不需要担心任何配置文件。这也将导致更好的测试。假设您想测试您的代码,而不是 WCF 服务本身。最好在您的测试中模拟它的行为,这样您就可以测试您的代码。

关于c# - 如何对我的 WCF 客户端类进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12041827/

相关文章:

.net - 在 Visual Studio 2013 上生成序列化程序集

c# - 如何获取 Windows 字体文件夹的路径?

c# - 很难让 Visual Studio 2010 附加到远程进程

C++ 程序不使用 Clang 和 visual Studio 2010 Express 编译

c# - 带有在线 MySql/Sql 数据库的 Xamarin Android 应用程序

c# - 单元测试中的 OpenRasta 模拟 HttpContext

c# - Java 中的 vb.net 'friend' 关键字等价于什么?

c# - 设计模式中枚举的替代方案

c++ - 如何将 cmake 项目转换为 Visual Studio 等效项目?

c# - 以小写字母开头的 C# 类名