简体   繁体   English

对VBScript进行模拟的C#单元测试

[英]C# unit test with mocking for VBScript

I have made changes to a VBScript (.vbs) that needs testing. 我对需要测试的VBScript(.vbs)进行了更改。

These are some VB6 classes defined in .cls files. 这些是.cls文件中定义的一些VB6类。 The VBScript requires the objects of these classes. VBScript需要这些类的对象。

My changes are limited to only one function in the VBScript that requires an object for just one of the classes. 我所做的更改仅限于VBScript中仅一个函数,该函数只需要一个类的对象。

I want to avoid having to run the entire script and initiate all the objects. 我想避免必须运行整个脚本并启动所有对象。

Is there any way, I can write unit tests to test my part of the script and mock the object that I need to pass? 有什么办法可以编写单元测试来测试脚本的一部分并模拟需要传递的对象?

This is how I am using C# unit tests to mock VB6 COM classes using RhinoMocks: 这就是我使用C#单元测试使用RhinoMocks模拟VB6 COM类的方式:

  1. Registered the COM component (ABC.dll) on my machine using regsvr32. 使用regsvr32在我的计算机上注册了COM组件(ABC.dll)。 This also required me to register all the types and assemblies that were being used by the VB6 classes. 这也需要我注册VB6类正在使用的所有类型和程序集。
  2. I then added reference to the COM component (ABC) by Add Reference > COM > ABC 然后,我通过添加引用> COM> ABC添加对COM组件(ABC)的引用
  3. I am using .Net 4.5.1 Framework and when I add reference to a COM component, it creates an interop assembly for me (Interop.ABC.dll) 我正在使用.Net 4.5.1 Framework,当我添加对COM组件的引用时,它将为我创建一个Interop程序集(Interop.ABC.dll)
  4. Right click the properties of the dll and ensure that Embed Interop Types is set to True. 右键单击dll的属性,并确保将“嵌入互操作类型”设置为True。 This allows all the unknown datatypes in the COM interface to be exposed as dynamic. 这允许将COM接口中的所有未知数据类型公开为动态。 This also allows you to use the interop library without having the types within registered on your machine. 这也使您可以使用互操作库,而无需在计算机上注册内部类型。
  5. I can now use the COM interfaces exposed through the interop library using mocking frameworks. 现在,我可以使用通过模拟框架通过互操作库公开的COM接口。 I started with using NSubstitute. 我开始使用NSubstitute。 However, since I have Embed Interop Types as true, most of my exposed interface properties are of the type dynamic (as opposed to object). 但是,由于我将Interop Types设置为true,所以大多数公开的接口属性都是动态类型(与object相对)。 NSubstitute does not support mocking dynamic get properties (I was able to mock dynamic set properties though). NSubstitute不支持模拟动态获取属性(尽管我能够模拟动态集合属性)。 Hence, I am using RhinoMocks. 因此,我正在使用RhinoMocks。
  6. I am using the Microsoft Script Control library to load the script and run the method within to be unit tested 我正在使用Microsoft脚本控制库加载脚本并在其中进行方法测试以进行单元测试

Note: I am new to COM :) So if you are an experienced COM user, please feel free to edit my answer for corrections or providing more details, links, etc. 注意:我是COM的新手:)因此,如果您是经验丰富的COM用户,请随时编辑我的答案以进行更正或提供更多详细信息,链接等。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM