Creating unit test for Sub with array passing ByRef in VB

Heloo!

I have a problem with generating unit test for procedure/funcion in VB project. When I choose "Create unit test" for such procedure I get an error in the test body:

Module MyModule

Public Sub MyFunction(ByRef Arr() As Boolean)
ReDim Arr(UBound(Arr))
End Sub

End Module

Test generated by VS is:

'''<summary>
'''A test for MyFunction(ByRef Boolean())
'''</summary>

<TestMethod()> _
Public Sub MyFunctionTest()

'Unit Test Generation Error: A private accessor could not be created for VBClassLibrary.MyModule.MyFunction: Error HRESULT E_FAIL has been returned from a call to a COM component.
Assert.Fail("Unit Test Generation Error: A private accessor could not be created for VBClassLi" & _
"brary.MyModule.MyFunction: Error HRESULT E_FAIL has been returned from a call to" & _
" a COM component.")

End Sub

Creating private accessor for this sub doesn't generate it also. I noticed that in C# project such problem doesn't occurs.
Is it possible to create unit test for subs as I mentioned in subject

Regards
marcin.walus



Answer this question

Creating unit test for Sub with array passing ByRef in VB

  • sm1else

    The root cause seems to be that we do not correctly handle byref arrays correctly in VB.

    I've entered a bug on our side to make sure we get this fixed.

    The reason this works ok in C# with something like:

    public void foo(ref int[] arr) { }

    is because the two languages slightly hand out the information differently. One of them says 'this is a reference, and it refers to an array'. The other says 'this is a reference to an array'. In other words, one of them is taking a bit of a short cut in the info.

    There are probably 2 work arounds for you - but neither is great. One is to make it ByVal just long enough to generate the test, and then add your own code to test the 'ByRef nature of the parameter'.

    The other is to generate a quick C# class library and make single method that looks like what you did in VB, generate the test and cut/paste that code into you real test project.

    Joe



  • rSchild

    Just to let you know I've reproduced the problem. When I get in to work tomorrow I'll find out what's going on and if there is a reasonable way for you to work around it.

    Joe



  • Creating unit test for Sub with array passing ByRef in VB