Smocks is an experimental framework for "static mocking" for .NET 4 and .NET 4.5. It is not a full-featured mocking framework, but rather a supplement to existing frameworks such as moq. These frameworks typically do not support mocking of static or non-virtual methods and properties. Smocks fills the gap.
Smocks uses some magic under the hood to mock the normally unmockable. This magic has its (technical) limitations though. Therefore it's very important that you play by the rules. Most importantly: avoid using variables defined outside the scope of Smocks.Run
unless you know what you're doing. These are some valid scenarios:
Smock.Run(context =>
{
context.Setup(() => DateTime.Now).Returns(new DateTime(2000, 1, 1));
// Outputs "2000"
Console.WriteLine(DateTime.Now.Year);
});
Smock.Run(context =>
{
int fortytwo = 42;
context.Setup(() => int.TryParse("forty-two", out fortytwo)).Returns(true);
int outResult;
bool result = int.TryParse("forty-two", out outResult);
// Outputs "True, 42"
Console.WriteLine("{0}, {1}", result, outResult);
});
Smock.Run(context =>
{
context.Setup(() => It.IsAny<string>().Equals("Bar")).Returns(true);
// Outputs "True"
Console.WriteLine("Foo".Equals("Bar"));
});
Smock.Run(context =>
{
context.Setup(() => It.IsAny<string>().Length).Returns(42);
// Outputs "42"
Console.WriteLine("Four".Length);
});
Available on NuGet:
PM> Install-Package Smocks
Strategies for deciding which assemblies to rewrite.Returns((arg1, arg2) => ...)
.Callback((arg1, arg2) => ...)
MatchingIt.Is<T>(x => ...)
- Strong-named assemblies
.SetupSet(() => ...)
- Support for mocking events
This library is currently in alpha status. I expect plenty of bugs to still be present. Should you encounter any oddities, please submit an issue or pull request. Any feedback will be greatly appreciated.
- mono.cecil (MIT license): the mindblowing CIL reading/writing library that powers Smocks.
- moq: the awesome mocking framework that heavily inspired the syntax and functionality of Smocks.
- AppDomainToolkit: a great source of knowledge on the intricacies of
AppDomain
.