Telerik JustMock
public class Foo
{
private void DoPrivate()
{
throw new NotImplementedException();
}
}
...
[TestMethod]
public void ShouldInvokeNonPublicMemberWithMatcher()
{
Foo foo = new Foo();
int expected = 1;
// Arrange
Mock.NonPublic.Arrange<int>(foo, "PrivateEcho", Arg.Expr.IsAny<int>()).Returns(expected);
// Act
int actual = foo.Echo(5);
// Assert
Assert.AreEqual(expected, actual);
}
public class Foo
{
private int PrivateEcho(int arg)
{
return arg;
}
public int Echo(int arg)
{
return PrivateEcho(arg);
}
}
...
[TestMethod]
public void Echo_OnExecute_ShouldReturnTheExpectationsForPrivateEcho()
{
var expected = 1;
Foo foo = new Foo();
// ARRANGE - When the non-public function PrivateEcho() is called from the foo instance, with any integer argument,
// it should return expected integer.
Mock.NonPublic.Arrange<int>(foo, "PrivateEcho", ArgExpr.IsAny<int>()).Returns(expected);
// ACT
int actual = foo.Echo(5); // Echo calls the private method PrivateEcho
// ASSERT
Assert.AreEqual(expected, actual);
}
public class Foo
{
private static int PrivateStaticProperty { get; set; }
}
...
[TestMethod]
public void ShouldMockPrivateStaticProperty()
{
var expected = 10;
// Arrange
Foo foo = new Foo();
Mock.NonPublic.Arrange<int>(typeof(Foo), "PrivateStaticProperty").Returns(expected);
// Act
int actual = foo.GetMyPrivateStaticProperty();
// Assert
Assert.AreEqual(expected, actual);
}