UnitTest.TestBuilder.Core 1.1.0

dotnet add package UnitTest.TestBuilder.Core --version 1.1.0                
NuGet\Install-Package UnitTest.TestBuilder.Core -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="UnitTest.TestBuilder.Core" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add UnitTest.TestBuilder.Core --version 1.1.0                
#r "nuget: UnitTest.TestBuilder.Core, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install UnitTest.TestBuilder.Core as a Cake Addin
#addin nuget:?package=UnitTest.TestBuilder.Core&version=1.1.0

// Install UnitTest.TestBuilder.Core as a Cake Tool
#tool nuget:?package=UnitTest.TestBuilder.Core&version=1.1.0                

UnitTest.TestBuilder.Moq

Introduction

This is a concrete implementation of a test builder for Moq based on UnitTest.TestBuilder.Core

Please visit the repository to see implementations for other test frameworks.

Description

This is a test library that helps to write unit tests faster by abstractin out the boilerplate codes and let the developers focus on the actual logics of the unit tests instead.

How to use
  1. Create a Builder class that inherits from MoqBuilder abstract classes
public class TestClass
{
  private class Builder : MoqBuilder<TestObject>
  {
  }
}
  1. Define class properties in the Builder class.

All reference type properties including string will be dynamically created and assigned by MoqBuilder.

The TestObject is created using its public constructor that has the most parameters. If the parameter is the same type as any of the properties defined in the Builder, the property of the Builder will be passed in as the parameter to the constructor. Hence, the TestObject will have access to the properties defined in the Builder.

public class TestClass
{
  private class Builder : MoqBuilder<TestObject>
  {
       Mock<IServiceA> ServiceA {get; private set;}
  }
}
  1. You can optionally override the creation of the property objects in the constructor of the Builder class
  private class Builder : MoqBuilder<TestObject>
  {
      public IServiceA ServiceA {get; private set;}
      
      public Builder(IContainer container)
      {
         // override the creation of ServiceA
         ServiceA = new ServiceA();
      }
  }
  1. You can optionally use dependency injection for the creation of the properties. You can define a custom DI container using your favorite DI library that implements the IContainer interface.

  2. You can optionally override the creation of the TestObject by overriding the CreateObject method.

  private class Builder : MoqBuilder<TestObject>
  {
      public IServiceA ServiceA {get; private set;}
      
      /// You can pass in additional parameters to create your custom TestObject
      /// through the *args* paramater
      protected override TestObject CreateObject(params object[] args)
      {
        return new TestObject();
      }
  }
Examples

Check out the Examples in Unit Tests project for more details

    public interface IRateService
    {
        double GetRate();
    }

    public class RateCalculator
    {
        private readonly IRateService _rateService;

        public RateCalculator(IRateService rateService)
        {
            _rateService = rateService;
        }

        public double GetTodayRate()
        {
            return 2 * _rateService.GetRate();
        }

        public double GetTomorrowRate()
        {
            return 3 * _rateService.GetRate();
        }
    }

    [TestClass]
    public class ExampleTests
    {
        private class Builder : MoqBuilder<RateCalculator>
        {
            // This property will be auto-populated by the base builder
            public Mock<IRateService> RateService { get; private set; }

            public Builder() : this(null) { }

            public Builder(IContainer container) : base(container) { }
        }

        [TestMethod]
        public void GetTodayRate_WithRate_ReturnCorrectRate()
        {
            //arrange
            var builder = new Builder();

            //set up the getRate method to return 2 and as verifable
            builder.RateService.Setup(a => a.GetRate()).Returns(2).Verifiable(); 

            //the calculator will get a reference to the same RateService in the builder
            var calculator = builder.Build();

            //act
            var result = calculator.GetTodayRate();

            //assert
            Assert.AreEqual(4.0, result);

            //verify RateService.GetRate() was called
            builder.RateService.Verify();
        }

        [TestMethod]
        public void GetTomorrowRate_WithRate_ReturnCorrectRate()
        {
            //arrange
            var builder = new Builder();

            //set up the getRate method to return 2 and as verifable
            builder.RateService.Setup(a => a.GetRate()).Returns(2).Verifiable();

            var calculator = builder.Build();

            //act
            var result = calculator.GetTomorrowRate();

            //assert
            Assert.AreEqual(6.0, result);

            //verify RateService.GetRate() was called
            builder.RateService.Verify();
        }
    }

<br> <br> Last Updated: Jan-13-2020 (Jacky-Mo)

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on UnitTest.TestBuilder.Core:

Package Downloads
UnitTest.TestBuilder.Moq

A test library that helps to write unit tests faster by abstractin out the boilerplate codes and let the developers focus on the actual logics of the unit tests instead.

UnitTest.TestBuilder.FakeItEasy

A test library that helps to write unit tests faster by abstractin out the boilerplate codes and let the developers focus on the actual logics of the unit tests instead.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 1,281 1/13/2020
1.0.1 635 12/5/2019

Support .net standard 2.0 and 2.1
Renamed base class and interface