标签:span clu config ready provider isnull initial str add
Moq
// 假定我有一个 MyFactory 用来创建 MyInterface 实例
// 创建 MyFactory 的 Mock 对象
var mockFactory = new Mock<MyFactory>();
// 创建 MyInterface 的 Mock 实例
var mockInstance = new Mock<MyInterface>();
// 使用 Moq 实现如果调用 MyInstance.DoSomething(bool) 方法无论传入参数为何值一概抛出 MyException 异常
mockInstance.Setup(c => c.DoSomething(It.IsAny<bool>()))
.Throws(new MyException("my exception message"));
// 使用 Moq 实现 MyFactory 的 Mock 实例第一次调用 CreateInstance(string) 方法时返回 MyInterface 的 Mock 实例
// 第二次及以后调用则返回真正的 MyInstance 实例
mockFactory.SetupSequence(f => f.CreateInstance(It.IsAny<string>()))
.Returns(mockInstance.Object)
.Returns(new MyInstance("real object"));
client.Factory = mockFactory.Object;
Please refer to Moq Quickstart
Moq is intended to be simple to use, strongly typed (no magic strings!, and therefore full compiler-verified and refactoring-friendly) and minimalistic (while still fully functional!).