如何对 C 代码的托管包装器进行单元测试?
我将围绕一些 C 函数创建一个托管 C++ 包装器,以允许其在其他 .NET 解决方案中使用。我正在考虑提供一个非常简约的包装器,例如:
C 标头中的签名:
void DOSTH(const char*, short, long*);
公开的托管接口:
public void doSomething(String^ input, short param, [Out] long^ %result);
为此,我的解决方案将具有 C 标头,并将引用包含我正在构建的已编译 C API 的 .dll 。
作为 Visual Studio 新手,我不确定如何对其进行单元测试。是否可以模拟 .dll 以提供模拟实现?有没有一个库可以使这种任务变得容易?我是否应该采用特定的解决方案结构来使这变得更容易?
这方面的任何指导都会很棒。谷歌搜索让我想要了解有关托管包装器单元测试的更多信息。
I will be creating a Managed-C++ wrapper around some C functions to allow its use in other .NET solutions. I'm looking at providing a very minimalist wrapper, something like:
Signature in C header:
void DOSTH(const char*, short, long*);
Exposed managed interface:
public void doSomething(String^ input, short param, [Out] long^ %result);
To do so my solution will have the C headers and will reference the .dll that contains the compiled C API that I am building against.
As a Visual Studio newbie I'm unsure how I would unit test this. Is it possible to mock out the .dll to provide a mock implementation? Is there a library that would make this kind of task easy? Is there a particular solution structure I should aim for to make this easier?
Any guidance in this area would be great. Google searches have left me wanting for more info on unit testing a managed wrapper.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在某些情况下(我想到的是工具限制和/或依赖关系复杂性),使用外部框架模拟依赖关系是毫无疑问的。然后,有一种完全合法的手动编写模拟的技术(我认为这是在模拟框架流行之前做事情的方法)。
这基本上就是您想要做的 - 伪造依赖项,在您的情况下恰好是 C 库。框架无济于事 - 您可能想尝试手动方法。
创建一些简单的、伪造的实现(非常类似于存根,例如,无论输入参数如何,都只返回固定值 - 当然,可能比这更复杂),编译它,让它公开完全相同的标头/函数并在中引用它你的测试项目。这就是伪造(存根/模拟)背后的基本思想 - 一个对象假装是另一个对象。
听起来很简单,但我实际上并没有尝试过——持保留态度,更多的是作为你可以走哪条路的建议。这种方法的局限性(除了它实际上在技术上是否可行)是非常差/没有配置选项(因为额外的伪造 DLL 会像硬编码的存根一样 - 配置文件可以提供帮助,但这感觉就像...工作量太大?)。
In some cases (tools limitations and/or dependency complexity comes to my mind), mocking dependency using external frameworks is out of question. Then, there's totally legitimate technique of writing mocks manually (I think that was the way to do stuff before mocking frameworks grew in popularity).
And that's basically what you want to do - fake out dependency, which in your case happens to be C library. Frameworks can't help - you might want to try manual approach.
Create some simple, faked implementation (pretty much like a stub, eg. only returning fixed values regardless of input params - naturally, might be more sophisticated than that), compile it, let it expose exactly the same headers/functions and reference it in your test project. That's the essential idea behind faking (stubbing/mocking) - one object pretending to be another.
As simple as it sounds, I haven't actually tried that - take it with a grain of salt and more as a suggestion which way you could go. Limitation of this approach (apart from whether it actually is technically possible) is very poor/none configuration options (since the extra faked DLL would act like a hardcoded stub - configuration files could help, but that feels like... too much work?).
您是否只需要能够存根/模拟您的包装器,以便您的测试不依赖于本机 dll?
然后,您可以为包装器声明一个抽象基类,编写一个调用本机 dll 的实现,并编写另一个用于返回预设值的测试目的的实现。或者,您可以使用 Moq 或 Rhino.Mocks 来模拟您的包装器。
Do you only need to be able to stub/mock out your wrapper so that your tests don't rely on the native dll?
Then you can declare an abstract base class for your wrapper, write one implementation that calls the native dll and another one for testing purposes that returns canned values. Or you can use a framework like Moq or Rhino.Mocks to mock your wrapper.