Skip to main content

MockMe by connorivy

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: MockMe

The concrete type mocking library for .NET

Author: connorivy

NuGet: https://www.nuget.org/packages/MockMe/

You can find more details at https://github.com/connorivy/MockMe/

Source : https://github.com/connorivy/MockMe/

Original Readme

note

MockMeFull


Coverage Status

What is it?

MockMe is a library for mocking dependencies in your unit test projects. Unlike other libraries that can only mock interfaces and virtual methods, MockMe can mock sealed classes and non-virtual methods.

Getting Started

Imagine you have the following repository class

sealed class MyRepo
{
public int ExpensiveDatabaseCall() => // some code;
}

Download the MockMe NuGet package, then the source generators and the "MockMe.Mock" type will be available in your project. Then you can customize the behavior of the repository class as below.

using MockMe;

// use this syntax to trigger the source generator to make a mock of the provided type
// the 'mock' object will have 3 properties: Setup, Assert, and MockedObject
// hint: rebuild test project after writing this line or IntelliSense may not work correctly
var mock = Mock.Me(default(MyRepo));

// the mock.Setup object has an identical interface to the original object
// from there you can configure method behavior with 'Returns', 'Callback', 'Throws', etc
mock.Setup.ExpensiveDatabaseCall().Returns(99);

// the mock.MockedObject is a special instance of the mocked type which has the modified behavior
// other instances of the mocked type will have the original behavior
MyRepo myRepo = mock.MockedObject;
int result = myRepo.ExpensiveDatabaseCall();

Assert.Equal(99, result);

// the mock.Assert object also has an identical interface to the original object.
// you can use it to assert certain mock behaviors
mock.Assert.ExpensiveDatabaseCall().WasCalled();

Check out the Wiki for more examples.

Give it a Star

If you like this project, please give it a star!

About

note

Creating mocks for testing classes

How to use

Example ( source csproj, source files )

This is the CSharp Project that references MockMe

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

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

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

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="MockMe" Version="1.1.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MockData\MockData.csproj" />
</ItemGroup>

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX


Usefull

Download Example (.NET C# )

Share MockMe

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

In the same category (Tests) - 4 other generators

mocklis

MSTest

Ridge

Rocks