How do I Use Microsoft Fakes Shims to Unit Test tightly coupled classes in VB.NET 4 particularly for Constructors

StackOverflow https://stackoverflow.com/questions/18666346

Question

Microsoft Fakes is a relatively new framework, and I am also relatively new to ALL aspects of this question, so I need detailed answers as such.

I have been given the task of "Unit Testing" an application, we'll call it a Legacy Application, (by Michael Feathers definition) particularly with the Microsoft Fakes framework. The reason for my managers suggesting Fakes is it's ability to allow unit testing around external dependencies and APIs, specifically to remove database dependency.

Given that I have a weak background in VB.net, the .NET framework in general, and Unit testing in general, this task is somewhat overwhelming. Irregardless, I would like insight on using SHIMS to detour some of these internal class dependencies. The reason I am interested in SHIMS vs the faster and more easily implemented STUBS is because there are no Interfaces in this application (something else I am relatively unfamiliar with.) and interface is required for a STUB.

Now, for a more specific question given this background, I would like help with trying to unit test a class, say class Person who consists of "string Name" and type "Address address" and uses these as constructor parameters. Then, we may have that class Address is constructed with parameters "string streetNumber, "string street", and a user type "state State" with its own parameters...

public class Person()

  Private mName As String
  Private mAddr As myclass.Address

   Public Sub New(ByVal name As string, ByVal addr As Address)
                      mName = name
                      mAddr = addr
  End Sub

And elsewhere...

public class Address()

  Private mStreet As String
  Private mState As myclass.State

   Public Sub New(ByVal street As String, ByVal state As State)
                      mStreet = street
                      mState = state
  End Sub

And then there would be the state with whatever properties and methods it contains....

So, I would like to know how to build a unit test to isolate class Person from these other classes,(Address then State) so that if they are under dev or changed, the unit test for Person will still Pass... any suggestions???

Was it helpful?

Solution

Currently you don't seem to have any coupling issues, but are confused about the nature of Fakes. Stubs can be used on any type which can be extended or implemented, and should be preferred over shims when available, as shims tie you too closely to the implementation details.

In Address, I'd probably pass in a StubState, and into Person a StubAddress. Common TDD practice would dictate that you make State and Address into interfaces so that you don't require any external code before you write your test, but it might not be worth it on types this simple (which could probably be structs? I don't know VB that well.)

Your goal should be dumb tests. Tests which only test one thing. Stubs should be enough for to manage that in your case, since you are doing DI already.

The main use of shims is legacy code, where you can't get at the dependencies. That doesn't mean they are a good thing, but sometimes they're what you need.

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