NET的单元测试VBA-强类型COM对象 [英] Unit testing vba from .net - strongly typed COM objects

查看:98
本文介绍了NET的单元测试VBA-强类型COM对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Ray Vega在此线程上的评论,我正在尝试使用.net MS Test项目对vba进行单元测试:

I'm trying to unit test vba by using a .net MS Test project as per a comment by Ray Vega on this thread: Best way to test a MS Access application?

这是我到目前为止所拥有的:

Here is what I have so far:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Vbe.Interop;

namespace FarmTestSuite
{
    [TestClass]
    public class UnitTest1
    {

        Microsoft.Office.Interop.Access.Application oAccess = null;
        Microsoft.Vbe.Interop.VBProject vbProject = null;

        [TestInitialize]
        public void Setup()
        {
            oAccess = new Microsoft.Office.Interop.Access.Application();
            oAccess.OpenCurrentDatabase(@"\\MyFilePath\Farm.mdb", true);
            vbProject = oAccess.VBE.VBProjects.Item(1);
        }

        [TestMethod]
        public void TestMethod1()
        {
            Assert.AreEqual("clsTestClass",vbProject.VBComponents.Item("clsTestClass").Name);
        }
    }
}

但是,到目前为止,我意识到我的.net应用程序不了解clsTestClass类型,因此我无法实例化它,也无法调用其公共方法.有什么办法吗?

However, now that I've got this far, I realise that my .net application has no knowledge of the clsTestClass type, so I cannot instantiate it, nor call its public methods. Is there any way of doing this?

推荐答案

在这里可能是完全错误的,但是当使用Excel VBA做类似的事情时,我发现VBA类可以是私有"或公共不可创建的".如果使您的类成为公共类,则可以在标准模块中创建一个公共函数,该模块除了实例化您的类然后返回它外,什么也不做.然后,您可以使用对象obj = Application.Run(新函数的名称")或类似的对象来获取您的类.

Might be entirely wrong here, but when doing similarish stuff using Excel VBA I found that VBA classes could either be 'Private' or 'Public non createable'. If you make your class the public one, then you can create a public function in a standard module that does nothing except instantiate your class and then return it. Then you can use object obj = Application.Run("name of new function") or similar to get hold of your class.

这篇关于NET的单元测试VBA-强类型COM对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆