其它 完成

develop
bicijinlian 7 years ago
parent 00918ea2d0
commit 097e03e34a

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MoqStudy.MockModel
{
public class DemoHelper
{
}
public interface CommandBaseProtectedMembers
{
bool Execute(string arg);
}
public interface TeacherDoubling
{
decimal Doubling(decimal number);
}
}

@ -46,6 +46,7 @@
<Compile Include="DemoModel\Bar.cs" /> <Compile Include="DemoModel\Bar.cs" />
<Compile Include="DemoModel\Baz.cs" /> <Compile Include="DemoModel\Baz.cs" />
<Compile Include="DemoModel\CallBaseHelper.cs" /> <Compile Include="DemoModel\CallBaseHelper.cs" />
<Compile Include="DemoModel\DemoHelper.cs" />
<Compile Include="DemoModel\IFoo.cs" /> <Compile Include="DemoModel\IFoo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StudyModel\Teacher.cs" /> <Compile Include="StudyModel\Teacher.cs" />

@ -23,9 +23,9 @@ namespace MoqStudy.MockModel
return Id + GroupId; return Id + GroupId;
} }
protected virtual int GetAge(int age) protected virtual decimal Doubling(decimal number)
{ {
return age + 2; return number * 2;
} }
protected virtual bool Execute(int number) protected virtual bool Execute(int number)
@ -36,5 +36,13 @@ namespace MoqStudy.MockModel
} }
return number % 2 == 0; return number % 2 == 0;
} }
/// <summary>
/// 运行私有方法
/// </summary>
public virtual bool CompareWithDoubling(decimal number)
{
return number > Doubling(number);
}
} }
} }

@ -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 以某种方式获取模拟实例

Loading…
Cancel
Save