RSCG – mocklis

RSCG – mocklis
 
 

name mocklis
nuget https://www.nuget.org/packages/mocklis/
link https://mocklis.readthedocs.io/en/latest/getting-started/index.html
author Esbjörn Redmo

Generating mocks from classes for unit tests

 

This is how you can use mocklis .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0-preview-23577-04" />
    <PackageReference Include="Mocklis" Version="1.4.0-alpha.2" />
    <PackageReference Include="MSTest.TestAdapter" Version="3.2.0-preview.23623.1" />
    <PackageReference Include="MSTest.TestFramework" Version="3.2.0-preview.23623.1" />
    <PackageReference Include="coverlet.collector" Version="6.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\MockLisClock\MockLisClock.csproj" />
  </ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>

</Project>


The code that you will use is


namespace TestClock;

[MocklisClass]
public partial class TestMock : IMyClock
{

}



using Mocklis;

namespace TestClock;

[TestClass]
public class TestClock
{
    [TestMethod]
    public void TestMyClock()
    {
        var mockSetup = new TestMock();
        mockSetup.GetNow.Return(DateTime.Now.AddYears(-1));

        // When testing the mock like this you need to cast to the interface.
        // This is different from e.g. Moq where the mocked instance and the 'programming interface' are different things.
        // With Mocklis they are the same. The 99% case is where the mock is passed to another constructor as a dependency,
        // in which case there's an implicit cast to the interface.
        var mock = (IMyClock)mockSetup;
        var data = mock.GetNow();
        Assert.AreEqual(DateTime.Now.Year - 1, data.Year);

    }
}


global using Microsoft.VisualStudio.TestTools.UnitTesting;
global using MockTest;
global using Mocklis.Core;

 

The code that is generated is

// <auto-generated />

#nullable enable

namespace TestClock
{
    partial class TestMock
    {
        public global::Mocklis.Core.FuncMethodMock<global::System.DateTime> GetNow { get; }

        global::System.DateTime global::MockTest.IMyClock.GetNow() => GetNow.Call();

        public global::Mocklis.Core.FuncMethodMock<global::System.DateTime> GetUtcNow { get; }

        global::System.DateTime global::MockTest.IMyClock.GetUtcNow() => GetUtcNow.Call();

        public TestMock() : base()
        {
            this.GetNow = new global::Mocklis.Core.FuncMethodMock<global::System.DateTime>(this, "TestMock", "IMyClock", "GetNow", "GetNow", global::Mocklis.Core.Strictness.Lenient);
            this.GetUtcNow = new global::Mocklis.Core.FuncMethodMock<global::System.DateTime>(this, "TestMock", "IMyClock", "GetUtcNow", "GetUtcNow", global::Mocklis.Core.Strictness.Lenient);
        }
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/mocklis