简体   繁体   English

使第三方库实现接口的策略?

[英]Strategy to make a third-party library implement interfaces?

I would like to write unit tests for some of my classes. 我想为我的一些课程编写单元测试。 Some of my classes depend on a third-party library which uses the file system and has no interfaces to mock. 我的一些类依赖于使用文件系统并且没有模拟接口的第三方库。

I'd to mock the class to avoid its dependencies on the filesystem, since my code is really only concerned about the results of that code. 我要模拟该类以避免它依赖于文件系统,因为我的代码实际上只关心该代码的结果。

What is the best strategy to use to mock the concrete classes of the library without modifying the initial library? 在不修改初始库的情况下,用于模拟库的具体类的最佳策略是什么?

I'm thinking I might create a wrapper object that implements an interface and contains the initial library's object. 我想我可能会创建一个实现接口并包含初始库对象的包装器对象。 However, I want to make sure there's not maybe a better way before I start down this path. 但是,我想确保在开始这条道路之前可能没有更好的方法。

Or, Would a tool like TypeMock be better suited than Moq in this case? 或者,在这种情况下,像TypeMock这样的工具是否比Moq更适合?

Background: 背景:

Unless it's a stable library/framework like the .NET framework, I prefer to decouple my code from it. 除非它像.NET框架一样是一个稳定的库/框架,否则我更愿意将代码与它分离。 That is, I like to make the library depend on my system rather than the opposite. 也就是说,我喜欢让库依赖于我的系统,而不是相反。

Edit, refrase: You might consider the library you are using "stable". 编辑,重新删除:您可能会认为您使用的库“稳定”。 However, since it interacts with an "external" system (the file system), I would probably still want to decouple my system from it. 但是,由于它与“外部”系统(文件系统)交互,我可能仍然希望将我的系统与其分离。

To accomplish this, I create an adapter/wrapper for the library. 为此,我为库创建了一个适配器/包装器。 The interface has the methods that my system wants the library to have, not the ones that the library happens to provide. 该接口具有我的系统希望库具有的方法,而不是库碰巧提供的方法。 The interface uses types that my system owns, not any from the library. 该接口使用我的系统拥有的类型,而不是库中的任何类型。 The adapter makes any convertions necessary. 适配器使任何转换成为必要。

This I do whether I want to mock/stub/fake the library or not because it provides a good separation of concerns and also it protects my system against changes in the library. 我这样做是否要模拟/存根/伪造库,因为它提供了良好的关注点分离,并且它还保护我的系统免受库中的更改。

Answer to your question: 回答你的问题:

Once the adapter/wrapper is there it's easy to fake it in your tests. 一旦适配器/包装器在那里,很容易在测试中伪造它。 As a bonus, since the adapter uses your system's language, it will be easier to write tests that are easy to read and understand. 作为奖励,由于适配器使用您的系统语言,因此编写易于阅读和理解的测试会更容易。

Whether you use a mock framework or write your own fakes for the adapeter is a matter of taste. 无论你是使用模拟框架还是为adapeter编写自己的假货都是一种品味问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#-从第三方库导入类并使其成为派生类(或类似的东西) - C# - import class from third-party library and make it a derived class (or something similar) 第三方实现DbContext,如何实现IdentityDbContext - Third-party implementation of DbContext, how to also implement IdentityDbContext 如何使用第三方库正确实现一次性模式 - How to correctly implement disposable pattern with third-party libraries 侦听第三方.NET库中的属性值更改 - Listening for property value change in third-party .NET library Roslyn:获取第三方库中定义的类型的符号 - Roslyn: get the symbol for a type defined in a third-party library 从第三方应用程序调用类库时缺少MissingMethodException - MissingMethodException when calling to a class library from third-party application 麻烦的GC.collect()调用第三方库 - Troublesome GC.collect() calls in third-party library 拦截并重写第三方库中的URL - Intercepting and re-writing a URL from third-party library 中止第三方库代码中执行的最安全方法 - The safest way to abort an execution in a third-party library code 拒绝客户端代码访问.NET中的第三方库 - Deny client code access to a third-party library in .NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM