|
|
@ -1138,25 +1138,73 @@ namespace MoqStudy.Test
|
|
|
|
[Fact]
|
|
|
|
[Fact]
|
|
|
|
public void Miscellaneous_Protected_Test()
|
|
|
|
public void Miscellaneous_Protected_Test()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//todo:用法还不清楚
|
|
|
|
var mock = new Mock<Teacher>() { CallBase = true };
|
|
|
|
var mock = new Mock<Teacher>();
|
|
|
|
var protectValue = 5;
|
|
|
|
mock.Protected()
|
|
|
|
mock.Protected()
|
|
|
|
.Setup<int>("GetAge",2)
|
|
|
|
.Setup<decimal>("Doubling", It.IsAny<decimal>())
|
|
|
|
.Returns(5);
|
|
|
|
.Returns(protectValue);
|
|
|
|
//todo:设置好了,怎么使用是个问题
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if you need argument matching,
|
|
|
|
// 如果用到了参数匹配, 必须使用 ItExpr 来代替 It
|
|
|
|
// you MUST use ItExpr rather than It planning on improving this for vNext
|
|
|
|
// 以后计划改进
|
|
|
|
// (see below for an alternative in Moq 4.8)
|
|
|
|
|
|
|
|
mock.Protected()
|
|
|
|
mock.Protected()
|
|
|
|
.Setup<bool>("Execute", ItExpr.IsAny<int>())
|
|
|
|
.Setup<bool>("Execute", ItExpr.IsAny<int>())
|
|
|
|
.Returns(true);
|
|
|
|
.Returns(true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//注意使用方法:
|
|
|
|
|
|
|
|
//设置模拟对象的保护成员后,直接访问不了(保护级别限制)
|
|
|
|
|
|
|
|
// 使用验证方法进行验证,或者在其它设置里直接使用保护方式的变量结果。
|
|
|
|
|
|
|
|
mock.Setup(t => t.CompareWithDoubling(It.IsAny<decimal>()))
|
|
|
|
|
|
|
|
.Returns((decimal p)=> { return p > protectValue; });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var runValue1 = mock.Object.CompareWithDoubling(1);
|
|
|
|
|
|
|
|
var runValue3 = mock.Object.CompareWithDoubling(3);
|
|
|
|
|
|
|
|
var runValue5 = mock.Object.CompareWithDoubling(5);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var runValue7 = mock.Object.CompareWithDoubling(7);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.False(runValue1);
|
|
|
|
|
|
|
|
Assert.False(runValue3);
|
|
|
|
|
|
|
|
Assert.False(runValue5);
|
|
|
|
|
|
|
|
Assert.True(runValue7);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 接口方式,设置保护成员
|
|
|
|
|
|
|
|
/// 4.8及更高版本
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void Miscellaneous_Protected_UseInterface_Test()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var mock = new Mock<Teacher>() { CallBase = true };
|
|
|
|
|
|
|
|
var protectValue = 5;
|
|
|
|
|
|
|
|
mock //保护方法转为接口,进行设置
|
|
|
|
|
|
|
|
.Protected()
|
|
|
|
|
|
|
|
.As<TeacherDoubling>()
|
|
|
|
|
|
|
|
.Setup(m=>m.Doubling(It.IsAny<decimal>()))
|
|
|
|
|
|
|
|
.Returns(protectValue);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//注意使用方法:
|
|
|
|
|
|
|
|
//设置模拟对象的保护成员后,直接访问不了(保护级别限制)
|
|
|
|
|
|
|
|
// 使用验证方法进行验证,或者在其它设置里直接使用保护方式的变量结果。
|
|
|
|
|
|
|
|
mock.Setup(t => t.CompareWithDoubling(It.IsAny<decimal>()))
|
|
|
|
|
|
|
|
.Returns((decimal p) => { return p > protectValue; });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var runValue1 = mock.Object.CompareWithDoubling(1);
|
|
|
|
|
|
|
|
var runValue3 = mock.Object.CompareWithDoubling(3);
|
|
|
|
|
|
|
|
var runValue5 = mock.Object.CompareWithDoubling(5);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var runValue7 = mock.Object.CompareWithDoubling(7);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.False(runValue1);
|
|
|
|
|
|
|
|
Assert.False(runValue3);
|
|
|
|
|
|
|
|
Assert.False(runValue5);
|
|
|
|
|
|
|
|
Assert.True(runValue7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 高级特性
|
|
|
|
#region 高级特性
|
|
|
|
[Fact]
|
|
|
|
[Fact]
|
|
|
|
public void Advanced_GetFromMocked_Test()
|
|
|
|
public void Advanced_GetMockFromMocked_Test()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// get mock from a mocked instance
|
|
|
|
// get mock from a mocked instance
|
|
|
|
IFoo foo =new Mock<IFoo>().Object; // get mock instance somehow 以某种方式获取模拟实例
|
|
|
|
IFoo foo =new Mock<IFoo>().Object; // get mock instance somehow 以某种方式获取模拟实例
|
|
|
|