Question

I have been using strictly Microsoft Fakes for Unit Testing. (therefore, I do not want nUnit or other examples.) I am able to create a Stub for a class which I previously thought would not be possible under the condition that classes need to implement interface to be stubbed. I believe I can create the stub due to the use of dependency injection, although I am not sure... (if anyone has more information on this It would be highly appreciated.)

Although that in itself may be the issue, I would like to see an example with the syntax to Stub a property Getter or Setter with VB.net and Microsoft Fakes. Microsoft does not have nearly as many hard examples of using Fakes with VB.net (just C#) and this has cost me hours of trying to determine the difference, as Vb.net itself is also somewhat new for me.

In this example specifically, we have a large class called Plan which in this specific case has a property Clusters which is of type ClusterCollection. I would like to Stub the Plan.Clusters getter to return a self defined ClusterCollection. This is the code I have tried to write to perfrom this and It has not worked...

    Dim cc As New ClusterCollection   
       'I would add elements to CC here.

    Dim myPlan As New StubPlan
    With myPlan
        .ClustersGet= _
            Function()
                Return cc
            End Function
    End With

99% sure this is not the way to do it but intellisense isn't helping me much either. Help???

Was it helpful?

Solution

My experience is with C#, but I think I can answer your questions. First, Fakes can stub anything that can be implemented or extended.

Stubs and Shims work by creating a public property of type Action or Func with the correct arguments and return type, and overrides methods to execute said delegate. You can therefore pass in any matching delegate, as a MethodGroup or as a lambda or traditional delegate.

Judging from the documentation, you are syntactically correct. How are you using the stub? Stubs are only one instance. If your goal is to override ClustersGet on any plan that appears, rather than an instance you know will be used, consider using shims or redesigning your method to support dependency injection, namely passing it the Plan object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top