0

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???

a-1
  • 696
  • 5
  • 10

1 Answers1

0

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.

Magus
  • 1,302
  • 9
  • 16
  • I am intending for it to be only this instance which returns something I explicitly define. This is the main issue why I am using shims in my Unit Testing vs. using Stubs. I could replace most all of my shims with stubs if I could better understand how pass the delegate. Given that none of these classes implement an interface, could that be the issue? Yet I am able to create stub objects(as seen above) and pass them into constructors.. – a-1 Oct 28 '13 at 19:14
  • A stub just inherits or implements whatever object it's based on, and declares properties with delegates for every method. No more to it than that. An interface has a bit more freedom, since a stub can only override virtual methods of course. An interface is in no way required. – Magus Oct 28 '13 at 20:12
  • why when i try this syntax (as shown above and MSDN) I can only access property "Clusters", which gets or sets, and no stubbed getter or setter is available? Nor stubbed methods...as they do not have appended parameters. In fact, it looks very much the same as if it were regular "Plan"... How exactly would I know if my Plan class can be implemented or extended? I cannot figure out how I am in some middle ground area where I can create stubs but not really use them for anything other than saving time – a-1 Oct 30 '13 at 20:17
  • 1
    If no get and set delegates are created, that implies that those aren't members of the immediate base class or are not virtual. In .NET, members are not virtual by default and therefore cannot be overridden unless explicitly set to virtual using the virtual keyword. – Magus Oct 30 '13 at 20:48