问题
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