SimpleMock 1.0.4

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

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

SimpleMock

SimpleMock is a very simple mocking framework, something that I have always wanted to try to write but I was prompted to have a go after a discussion at work.

Usage

The examples below use the following interface as an example

public interface IWorker
{
    int DoSomething(int anInt, string aString, bool aBool);
    string DoSomethingStringy(int anInt);
    int Height { get; }
}

Creating the Mock

var workerMock = new Mock<IWorker>();

Mocking a Method

workerMock
    .Setup(w => w.DoSomething(0, "", false))
    .Returns(5);

The following two code blocks will return 5 for method calls that passed an empty string for the second parameter regardless of the values of the other two parameters.

workerMock
    .Setup(w => w.DoSomething(It.IsAny<int>(), "", It.IsAny<bool>()))
    .Returns(5);
workerMock
    .Setup(w => w.DoSomething(It.IsAny<int>(), It.Affirms<string>(s => s == ""), It.IsAny<bool>()))
    .Returns(5);

Mocking a Readable Property

workerMock
    .Setup(w => w.Height)
    .Returns(10);

Supplying the Mock to a Dependant

var dependant = new Dependant(workerMock.MockObject);

Getting the Call Count

workerMock.GetCallCount(w => w.DoSomething(0, "", false));

The following code will only match method calls that passed an empty string for the second parameter regardless of the values of the other two parameters.

workerMock.GetCallCount(w => w.DoSomething(It.IsAny<int>(), "", It.IsAny<bool>()));

Getting the Parameter Values of a Specific Call

workerMock.GetCallParameters(w => w.DoSomething(0, "", false), 7);

N.B. The index is zero-based.

The following code will only match method calls that passed 59 for the first parameter regardless of the values of the other two parameters.

workerMock.GetCallParameters(w => w.DoSomething(59, It.IsAny<string>(), It.IsAny<bool>()), 2);

Getting the Count of Set Operations on a Read/Write Property

workerMock.GetSetCallCount(w => w.Height, () => It.IsAny<int>());

Getting the Parameter Values of Set Operations on a Read/Write Property

workerMock.GetSetCallParameters(w => w.Height, () => It.Affirms<int>(h => h < 10), 7);

N.B. The index is zero-based.

Limitations

This is only a very simple and niave implementation so there are a number of limitations, amongst which are:

  • No support for set only parameters
    • This is much harder to implement. It involves decompiling and intepretring a call to the set code as a set property call is not a legal expression that can be interpreted as an expression tree.
  • Can only mock interfaces
  • No support for callbacks
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

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 154 9/4/2023
1.0.3 135 9/1/2023
1.0.2 145 8/31/2023
1.0.1 153 8/30/2023
1.0.0 157 8/30/2023