0

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?

Community
  • 1
  • 1
majjam
  • 1,286
  • 2
  • 15
  • 32

1 Answers1

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.

steveo40
  • 931
  • 5
  • 11
  • Thanks for this, I can indeed use the above to access my code from within .net. Though I'm thinking something more substantial (PIA?) will be required for me to see the attributes of my classes from .net. – majjam Oct 27 '14 at 17:07
  • You could reference the Visual Basic Editor (VBE) and interact with the modules & classes that way. Might need to check that permission is given to access the VBE programmatically though (I think you can do it via the registry). – steveo40 Oct 29 '14 at 21:23
  • Thanks Steve, I think that's what I'm doing in the above code, I just cannot get details of my custom types (to be able to use asserts on my public methods for e.g.). What I'm really interested in knowing is how my .net test app can get this information, atm I'm thinking that an Interop Assembly might be the answer. – majjam Oct 30 '14 at 09:41