locked
Microsoft Fakes VB.Net Shadows method RRS feed

  • General discussion

  • I am trying to create a unit test using Microsoft's Fakes. The issue I have uncovered is with the vb.net key work "Shadows".

    Allow me to paint you the code base issue. We have a class called BaseView. Another class called CommodityView which inherits from the BaseView. On the BaseView we have a method called InitializeView that takes in an object parameter. Code snippet:

        Public Overridable Sub InitializeView(ByVal dto As Object)
            '// setup databinding with your dto
        End Sub
    

    The CommodityView "Shadows" the InitializeView method with a new signature. Code snippet:

        

        PublicShadowsSub InitializeView(ByVal dto AsObjectByVal startingIndex AsIntegerByVal isReadOnly AsBoolean)

    stuff happens here

           EndSub

    I did the normal add fakes to the test project. I went to the f.csproj for the assembly I am trying to create a fake for and tried to compile it. It would not compile because of the following errors:

    Error 1 No overload for 'InitializeView' matches delegate 'Microsoft.QualityTools.Testing.Fakes.FakesDelegates.Action<object>' C:\gerald\POC\LearningFakesShadow\LearningFakesShadowTests\obj\Debug\Fakes\lfs\f.cs 351 108 f
    Error 2 No overload for method 'InitializeView' takes 1 arguments C:\gerald\POC\LearningFakesShadow\LearningFakesShadowTests\obj\Debug\Fakes\lfs\f.cs 360 17 f

    We have discussed this error in a previous thread (http://social.msdn.microsoft.com/Forums/en-US/d315282f-513d-4041-84d9-55bd554fa024/fakes-code-generation-for-an-overrides-method-creates-error?forum=vsunittest). So I took the liberty of adding the following code snippet to the CommodityView class:

        Public Shadows Sub InitializeView(ByVal dto As Object)
            MyBase.InitializeView(dto)
        End Sub

    We are able to compile the f.csproj. Yeah! Or, so we thought. The fake assembly we were expecting did not get added to the test project's references. The fakes assembly did get created in the folder structure .\obj\Debug\Fakes\lfs\o but was not auto-magically added as anticipated. Boo! Ah! I am able to manually browse to the fakes assembly and add it to the test project's references. But, I want it auto-magically added to the test project's references.

    By the way, this is reproducible.

    • Create a new project (I choose a console app).
    • Create a base class (I called mine BaseClass - I know how original).
    • Within the base class create a method that is overridable (I called my method InitializeView with a dto as object parameter)
    • Create another class that inherits the base class (I called mine SomeClass - I know, I know, but stay with me)
    • Within the this new class have a method that Shadows the base class method (I have the InitializeView with dto as object, startingIndex as integer, and isReadOnly as Boolean parameters)
    • Create your test project
    • Try to compile the solution (you will get the error listed earlier and see other thread listed earlier to resolve)
    • You will need to go back to the new class (recall mine is called SomeClass) and add a new method that has the same signature as the base class but with the key word Shadows. (I still need to figure out what the behavior will be now with this new shadow code.)
    • Compile the solution
    • Look at the references to see if the fakes assembly was added - boo! no it wasn't

    Code snippets:

    Public Class BaseClass
     
        Public Overridable Sub InitializeView(ByVal dto As Object)
     
        End Sub
     
    End Class
    
    Public Class SomeClass
        Inherits BaseClass
     
        Public Shadows Sub InitializeView(dto As Object, startingIndex As Integer, isReadOnly As Boolean)
     
        End Sub
     
        Public Shadows Sub InitializeView(ByVal dto As Object)
     
        End Sub
     
        Public Sub DoSomething()
     
        End Sub
     
    End Class
    

    So, what are my options outside of manually adding the fakes assembly (and keeping tribal knowledge on the test project)?

    Cheers,

    • Changed type Gerald Bauer Friday, December 13, 2013 8:45 PM have a work around
    Thursday, December 12, 2013 4:27 PM

All replies

  • I spoke with others that know the code base better. We decided to remove the shadows and replace it with overloads since it makes more sense from an architecture perspective. (Some developer was just following Microsoft's compiler recommendation so that they could move forward.)
    Friday, December 13, 2013 8:45 PM