GithubHelp home page GithubHelp logo

snerte / bunit.automocker Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 33 KB

This project combines Moq.AutoMocker and bunit

License: GNU General Public License v3.0

C# 43.41% HTML 26.86% CSS 29.73%

bunit.automocker's Introduction

This project combines bunit and Moq.AutoMock making it super easy to create tests for Blazor Components. All dependencies are mocked automatically with AutoMocker so that you only have to setup the dependencies you use! Making test code super clean, easy to read and write.

Sample

Given Routes.razor.cs

public partial class Routes
{
    [Inject]
    IMydependency MyDependency { get; set; } = null!;

    [Inject]
    IMyOtherDependency MyOtherDependency { get; set; } = null!;

    protected override void OnInitialized()
    {
        MyDependency.GetSomeValue();
    }
}

Problem

Without AutoMocker the test setup process is tedious and requires a lot of maintenance.

public class WithoutAutoMockerTests : TestContext
{
    [Fact]
    public void MyTest()
    {
        // Arrange
        var myDependency = new Moq.Mock<IMydependency>();
        myDependency.Setup(x => x.GetSomeValue()).Returns("Hello World");

        Services.AddSingleton<IMydependency>(myDependency.Object);

        var myOtherDependency = new Moq.Mock<IMyOtherDependency>();
        Services.AddSingleton<IMyOtherDependency>(myOtherDependency.Object);

        // Act
        var cut = RenderComponent<Routes>();

        // Assert
        Assert.Contains(cut.Markup, "Hello World");
    }
}

Every dependency needs to be setup, not setting up IMyOtherDepencency will cause the test to fail:

[Fact]
public void Fails_WhenAllOnlyUsedDependencyIsRegisterd()
{
    // Arrange
    var myDependency = new Moq.Mock<IMydependency>();
    myDependency.Setup(x => x.GetSomeValue()).Returns("Hello World");

    Services.AddSingleton<IMydependency>(myDependency.Object);

    // Act & Assert
    Assert.Throws<InvalidOperationException>(() => RenderComponent<Routes>());
}

Solution

With Automocker writing tests is simple. Change TestContext to AutoMockerTestContext and use the Mocker field from the baseclass to setup AutoMocker. For more info and AutoMocker recepies, check their docs

public class AutoMockerTestContextTests : AutoMockerTestContext
{
    [Fact]
    public void CreatesUnregisteredDependencies()
    {
        // Arrange
        Mocker.Use<IMydependency>(x => x.GetSomeValue() == "Hello World");

        // Act
        var cut = RenderComponent<Routes>();

        // Assert
        Assert.Contains(cut.Markup, "Hello World");
    }
}

bunit.automocker's People

Contributors

snerte avatar

Watchers

 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.