mocklis by Esbjörn Redmo
Nuget / site data
Details
Info
Name: mocklis
Mocklis is a library and source code generator for .net, targeted at generating test doubles from interfaces. This package contains tools for writing tests using the generated code.
Author: Esbjörn Redmo
NuGet: https://www.nuget.org/packages/mocklis/
You can find more details at https://mocklis.readthedocs.io/en/latest/getting-started/index.html
Source : https://github.com/mocklis/mocklis/
Original Readme
Mocklis is a library and source code generator for .net, targeted at generating test doubles from interfaces.
Useful Links:
Home page: https://mocklis.net
Github project page: https://github.com/mocklis
Documentation: https://mocklis.readthedocs.io
NuGet page: https://www.nuget.org/profiles/mocklis
About
Generating mocks from classes for unit tests
How to use
Example ( source csproj, source files )
- CSharp Project
- TestMock.cs
- TestClock.cs
- Usings.cs
This is the CSharp Project that references mocklis
<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>
This is the use of mocklis in TestMock.cs
namespace TestClock;
[MocklisClass]
public partial class TestMock : IMyClock
{
}
This is the use of mocklis in TestClock.cs
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);
}
}
This is the use of mocklis in Usings.cs
global using Microsoft.VisualStudio.TestTools.UnitTesting;
global using MockTest;
global using Mocklis.Core;
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- TestClock.TestMock.g.cs
// <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);
}
}
}
Usefull
Download Example (.NET C# )
Share mocklis
https://ignatandrei.github.io/RSCG_Examples/v2/docs/mocklis