Telerik JustMock
Mock.SetupStatic(typeof(MyStaticType));
Mock.SetupStatic(typeof(MyStaticType), StaticConstructor.Mocked);
The behavior of the static constructor to be executed only once remains.[TestMethod]
public void ShouldMockStaticClassWithStrictBehavior()
{
var expected = 10;
// ARRANGE
// Getting the static instance(Foo) ready for mocking disregarding the constructor.
// If we don't mock the constructor, a NotImplementedException will be thrown.
Mock.SetupStatic(typeof(Foo), StaticConstructor.Mocked);
// Arranging: When the Foo.Execute() method is called with 10, it should return expected.
Mock.Arrange(() => Foo.Execute(10)).Returns(expected);
// ACT
var actual = Foo.Execute(10);
// ASSERT
Assert.AreEqual(expected, actual);
}
public class Foo
{
static Foo()
{
throw new NotImplementedException();
}
public static int FooProperty
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
}
...
[TestMethod]
public void ShouldArrangeStaticProperty()
{
var expected = 0;
// ARRANGE
// Getting the static instance(Foo) ready for mocking disregarding the constructor.
// If we don't mock the constructor, a NotImplementedException will be thrown.
Mock.SetupStatic(typeof(Foo), StaticConstructor.Mocked);
// Arranging: When the static(Foo.FooProp_GET) property is called, it should return expected.
Mock.Arrange(() => Foo.FooProperty).Returns(expected);
// Assert
Assert.AreEqual(expected, Foo.FooProp);
}
public class Foo
{
static Foo()
{
throw new NotImplementedException();
}
public static int FooProperty
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
}
...
[TestMethod]
public void ShouldFakeStaticPropertySet()
{
bool isCalled = false;
// ARRANGE
// Getting the static instance(Foo) ready for mocking disregarding the constructor.
// If we don't mock the constructor, a NotImplementedException will be thrown.
Mock.SetupStatic(typeof(Foo), StaticConstructor.Mocked);
// Arranging: When the static(Foo.FooProp_SET) property is set to 10, it should assign true to isCalled instead.
Mock.ArrangeSet(() => { Foo.FooProperty = 10; }).DoInstead(() => { isCalled = true; });
// ACT
Foo.FooProp = 10;
// ASSERT
Assert.IsTrue(isCalled);
}
Extension methods are a special kind of static methods. They are called as if they were instance methods on the extended type. Mocking extension methods is similar to mocking any instance method. The only difference is that we don’t need a Mock.Create<T>() call to initialize the class for mocking as extension mocking is by default partial.
public static class FooExtensions
{
public static string Echo(this Foo foo, string value)
{
return value;
}
}
...
[TestMethod]
public void ShouldAssertExtensionMethodMockingWithArguments()
{
string expected = "bar";
// ARRANGE
var foo = new Foo();
// Arranging: When the extension method foo.Echo() is called with any string argument,
// it should return expected.
Mock.Arrange(() => foo.Echo(Arg.IsAny<string>())).Returns(expected);
// ACT
string actual = foo.Echo("hello");
// ASSERT
Assert.AreEqual(expected, actual);
}