ERNI.AutoMockExtended 1.0.4

dotnet add package ERNI.AutoMockExtended --version 1.0.4                
NuGet\Install-Package ERNI.AutoMockExtended -Version 1.0.4                
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="ERNI.AutoMockExtended" Version="1.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ERNI.AutoMockExtended --version 1.0.4                
#r "nuget: ERNI.AutoMockExtended, 1.0.4"                
#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 ERNI.AutoMockExtended as a Cake Addin
#addin nuget:?package=ERNI.AutoMockExtended&version=1.0.4

// Install ERNI.AutoMockExtended as a Cake Tool
#tool nuget:?package=ERNI.AutoMockExtended&version=1.0.4                

Known Compatibility Issue

SourceCodeGenerator have some known issue with VS 2019 and .Net5 and lower. In order to use this library your device should have .NET 6 installed and you're using Visual Studio 2022 or later. This library is not compatible with lower version of VS in my experience.

Normal Mocking vs AutoCreateMock

Example Interface

public interface ISample
{
     Task<int> GetInt(string input);
     Task<IEnumerable<MyEntity>> Search(string keyword, int pageSize, int pageIndex, string sorting);
}

Normal Mocking

var mock = new Mock<ISample>();
var result = new[] { new MyEntity("abc"), new MyEntity("def") };

mock.Setup(m => m.GetInt(It.IsAny<string>())).ReturnsAsync(20);
mock.Setup(m => m.Search(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>())).ReturnsAsync(result);

Improved Mocking (using AutoCreateMock)

var mock = new Mock<ISample>();
var result = new[] { new MyEntity("abc"), new MyEntity("def") };

mock.SetupGetInt(20);
mock.SetupSearch(result);

How To Use AutoCreateMock

Register the ISample to AutoMockCreate

use the attribute AutoCreateMock.AutoCreateMock to let the library know that it needs to generate Mock and AutoMock extensions for the specified interface.

public class Mocks
{
    [AutoCreateMock.AutoCreateMock]
    public ISample SampleInterface { get; set; }
}

Sample Usage

Using AutoMock with Setup

public async Task Returns1_ShouldReturn1()
{
     var mock = AutoMock.GetLoose();
     var target = mock.CreateSampleInterface();
     target.SetupGetInt(1);

     var result = await target.Object.GetInt("anystring");

     Assert.Equal(1, result);
}

Using AutoMock with Argument

public async Task Returns1_ShouldReturn1()
{
     var mock = AutoMock.GetLoose();
     var target = mock.CreateSampleInterface(getIntResult: 20);

     var result = await target.Object.GetInt("anystring");

     Assert.Equal(20, result);
}

Using Moq.Mock

public async Task Moq_Returns1_ShouldReturn1()
{
     var target = new Mock<ISample>();
     target.SetupGetInt("abc", 5);

     var result = await target.Object.GetInt("abc");

     Assert.Equal(5, result);
}
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 3,949 7/27/2022
1.0.3 495 7/26/2022