SvetlovA / static-mock

SMock is opensource lib for mocking static and instance methods and properties.
https://svetlova.github.io/static-mock/
MIT License
10 stars 3 forks source link
mock mocking moq static test testing tests unittesting unittests

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.