SMock 2.3.0

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

// Install SMock as a Cake Tool
#tool nuget:?package=SMock&version=2.3.0                

SMock

NuGet Version NuGet Download

SMock is opensource lib for mocking static and instance methods and properties. API Documntation

Installation

Download and install the package from NuGet or GitHub

Getting Started

Hook Manager Types

SMock is based on MonoMod library that produce hook functionality

Code Examples

Setup is possible in two ways Hierarchical and Sequential

Returns (Hierarchical)

Mock.Setup(context => StaticClass.MethodToMock(context.It.IsAny<int>()), () =>
{
    var actualResult = StaticClass.MethodToMock(1);
    ClassicAssert.AreNotEqual(originalResult, actualResult);
    ClassicAssert.AreEqual(expectedResult, actualResult);
}).Returns(expectedResult);

Mock.Setup(context => StaticClass.MethodToMock(context.It.IsAny<int>()), () =>
{
    var actualResult = StaticClass.MethodToMock(1);
    ClassicAssert.AreNotEqual(originalResult, actualResult);
    ClassicAssert.AreEqual(expectedResult, actualResult);
}).Returns(() => expectedResult);

Mock.Setup(context => StaticClass.MethodToMock(context.It.Is<int>(x => x == 1)), () =>
{
    var actualResult = StaticClass.MethodToMock(1);
    ClassicAssert.AreNotEqual(originalResult, actualResult);
    ClassicAssert.AreEqual(expectedResult, actualResult);
}).Returns<int>(argument => argument);

Mock.Setup(context => StaticClass.MethodToMockAsync(context.It.IsAny<int>()), async () =>
{
    var actualResult = await StaticClass.MethodToMockAsync(1);
    ClassicAssert.AreNotEqual(originalResult, actualResult);
    ClassicAssert.AreEqual(expectedResult, actualResult);
}).Returns<int>(async argument => await Task.FromResult(argument));

Other returns hierarchical setup examples

Returns (Sequential)

using var _ = Mock.Setup(context => StaticClass.MethodToMock(context.It.IsAny<int>()))
    .Returns(expectedResult);

var actualResult = StaticClass.MethodToMock(1);
ClassicAssert.AreNotEqual(originalResult, actualResult);
ClassicAssert.AreEqual(expectedResult, actualResult);

Other returns sequential setup examples

Throws (Hierarchical)

Mock.Setup(() => StaticClass.MethodToMock(), () =>
{
    Assert.Throws<Exception>(() => StaticClass.MethodToMock());
}).Throws<Exception>();

Other throws hierarchical setup examples

Throws (Sequential)

using var _ = Mock.Setup(() => StaticClass.MethodToMock()).Throws<Exception>();

Assert.Throws<Exception>(() => StaticClass.MethodToMock());

Other throws sequential setup examples

Callback (Hierarchical)

Mock.Setup(() => StaticClass.MethodToMock(), () =>
{
    var actualResult = StaticClass.MethodToMock();
    ClassicAssert.AreNotEqual(originalResult, actualResult);
    ClassicAssert.AreEqual(expectedResult, actualResult);
}).Callback(() =>
{
    DoSomething();
});

Mock.Setup(context => StaticClass.MethodToMock(context.It.IsAny<int>()), () =>
{
    var actualResult = StaticClass.MethodToMock(1);
    ClassicAssert.AreNotEqual(originalResult, actualResult);
    ClassicAssert.AreEqual(expectedResult, actualResult);
}).Callback<int>(argument =>
{
    DoSomething(argument);
});

Other callback hierarchical setup examples

Callback (Sequential)

using var _ = Mock.Setup(() => StaticClass.MethodToMock()).Callback(() =>
{
    DoSomething();
});

var actualResult = StaticClass.MethodToMock();
ClassicAssert.AreNotEqual(originalResult, actualResult);
ClassicAssert.AreEqual(expectedResult, actualResult);

Other callback sequential setup examples

Other examples

Library license

The library is available under the MIT license.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 is compatible.  net471 is compatible.  net472 is compatible.  net48 is compatible.  net481 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in 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
2.3.0 94 11/20/2024
2.2.0 5,480 2/8/2024
2.1.0 47,049 8/29/2023
2.0.0 230 7/16/2023
1.8.0 820 12/13/2022
1.7.1 2,007 8/27/2022
1.7.0 441 8/27/2022
1.6.0 616 12/21/2021
1.5.0 358 8/2/2021
1.4.0 361 7/18/2021
1.3.0 360 7/1/2021
1.2.1 357 5/17/2021
1.2.0 396 5/5/2021
1.1.0 357 4/30/2021
1.0.0 381 4/27/2021