Giter Club home page Giter Club logo

sociabletesting's Introduction

Autofac Sociable Testing

.NET Core

The main reason for creating this package is to make writing sociable unit tests easier which is often used in the Chicago style of TDD as oppose to mocking which is more heavily used in the London style. These are also known as classicist vs mockist testing styles.

This package can also be used as a tool to help write integration tests. Please read the link above about sociable unit tests to understand the difference between the two.

Note: This package only works with the Autofac container.

When using dependency injection writing a sociable unit test often ends up with something like this:

public UnitShould() {
    //common test setup
    sut = new Sut(
        new DependencyA(new DependencyB(), new DependencyC()),
        new DependencyD(),
        new DependencyE(new DependencyF())
        );    
}

whereas with test doubles it would look similar to:

public UnitShould() {
    //common test setup
    var mockA = new Mock<DependencyA>();
    var mockD = new Mock<DependencyD>();
    var mockE = new Mock<DependencyE>();
    
    sut = new Sut(mockA, mockD, mockE);    
}

Mocking means you only have to worry about the immediate set of dependencies rather than all of the layers.

Installation

This package can be installed via Nuget: https://www.nuget.org/packages/SociableTesting.Autofac/

Usage

With this package sociable unit tests can be setup without having to worry about initialising any of the dependencies. Instead, dependencies are initialised with the container just like they are in production. The examples below are for use with XUnit.

public class SociableTestShould
{
    [Fact]
    public void InitialiseRegisteredClass()
    {
        var setup = new SociableTest<Sut>(new MyModule());
        setup.Sut.Should().BeOfType<Sut>();
    }
}

It is not necessary to register all the modules in your application, if any registrations are missing when the container is built then a default mock will be used.

You can register further modules by calling setup.ProvideModule(new OtherModule());

Using Your Own Dependencies

Dependencies can be replaced in the container by using the ProvideDependency method. ProvideDependency must be used before any access to either the Sut or ContainerBuilder properties. Once either of these properties is accessed the container is built and ProvideDependency will no longer have any effect.

This is useful for setting up your own test doubles or fakes rather than relying on the default mocks provided.

public class SociableTestShould
{
    [Fact]
    public void InitialiseRegisteredClass()
    {
        var setup = new SociableTest<Class1>(new MyModule());
        var mock = new Mock<IMockedDependency>();
        ProvideDependency<IMockedDependency>(mock.Object);
        setup.Sut.Should().BeOfType<Class1>();
    }
}

Register on ContainerBuilder

The ContainerBuilder is accessible via a public property on SociableTest so you can make use of the full range of registration types Autofac provides.

Retrieving Instances from Container

setup.Container can be used to access IContainer directly to resolve instances in tests, such as accessing dependencies of the class under test.

sociabletesting's People

Contributors

sdavey149 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.