Unit testing vba from .net - strongly typed COM objects

杀马特。学长 韩版系。学妹 提交于 2019-12-11 20:42:51

问题


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);
        }
    }
}

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?


回答1:


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.



来源:https://stackoverflow.com/questions/26511396/unit-testing-vba-from-net-strongly-typed-com-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!