SMock is opensource lib for mocking static and instance methods and properties. API Documntation
Download and install the package from NuGet or GitHub
SMock is based on MonoMod library that produce hook functionality
Setup is possible in two ways Hierarchical and Sequential
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
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
Mock.Setup(() => StaticClass.MethodToMock(), () =>
{
Assert.Throws<Exception>(() => StaticClass.MethodToMock());
}).Throws<Exception>();
Other throws hierarchical setup examples
using var _ = Mock.Setup(() => StaticClass.MethodToMock()).Throws<Exception>();
Assert.Throws<Exception>(() => StaticClass.MethodToMock());
Other throws sequential setup examples
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
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
The library is available under the MIT license.