Stubs Vs Shims Difference

 

Hello everybody,

today I want to mention difference between two kinds of testing. Shim vs Stub. 

Take a look at the following code sample:

// Stub sample
public interface IDependency
{
    void SomeMethod();
}
 
// Take note that class TestTarget allows to inject 
// interface IDependency
 
public class TestTarget
{
    private IDependency _dependency { getset; }
    public TestTarget(IDependency dependency)
    {
        _dependency = dependency;
    }
 
    public void TestMethod()
    {
        _dependency.SomeMethod();
        
    }
}
 
// Shim sample. Take note that class ShimTarget doesn't allow to inject 
// interface as dependency
public class Dep : IDependency
{
    public void SomeMethod()
    {
        
    }
}
 
public class ShimTarget
{
 
    public void TestMethod()
    {
        Dep d = new Dep();
        d.SomeMethod();
    }
}

Take note that class ShimTarget has dependency from class Dep. And you will not be able to mock this dependency with any mocking framework. But class TestTarget allows you to use plenty of mocking frameworks in order to unit test. And if you'll see class similar to ShimTarget then know, you'll most probably need Shims from Microsoft Fakes in order to unit test it. 

No Comments

Add a Comment
 
Comments are closed