I am getting started with Unit testing in order to better manage this Legacy code in VB6 that I’m working on. I started with the C# unit test on a small demo application to get the hang of it and then started to play with SimplyVBUnit (which I’m still no proficient with).
What I immediately noticed is that if a create unit tests in VB6 and then migrate my code I will have to recreate\migrate my tests code on the .NET side. I tried resolve this by searching a way to do this via COM Interop to see if I could show my VB6 methods in a Unit Test Project in .NET but couldn’t find a good source of information.
What I want to ask then is if this approach is even viable? And if so, what are the steps to accomplish this?
UPDATE I: Partial success
After searching around I found that I could create an ActiveX dll in VB6 to pass a class. In order to test this I created a new VB6 project with the ActiveX Dll template called “MyTestProject” and in it created a class called CalculatorLib with the following code:
Public Function Addition(ByVal operand1 As Long, ByVal operand2 As Long) As Long
Addition = (operand1 + operand2)
End Function
Public Function Subtract(ByVal operand1 As Long, ByVal operand2 As Long) As Long
Subtract = (operand1 - operand2)
End Function
Public Function Divide(ByVal operand1 As Long, ByVal operand2 As Long) As Long
Divide = (operand1 + operand2)
End Function
Then create the dll by pressing make under File -> Make MyTestProject.dll
In Visual studio 2015 create a new Unit Test project. There is a naming convention for this but for this test I did not take that into account.
Select your project in the Solution explorer and right click and select Add and then reference. In the COM tab search for your dll and add it to you project. In the test I created a (quick and dirty) test method called TestAdd:
[TestMethod]
public void TestAdd()
{
MyTestProject.CalculatorLib calculator = new MyTestProject.CalculatorLib();
int io = calculator.Addition(10, 30);
Assert.AreEqual(io, 20);
}
This test fails, as expected, and changin the 30 to a 10 makes the test pass. JOY!
So now that I am able to do this how do I test the function and sub’s that are in my modules and forms without having to convert them in classes in a ActiveX dll?