C# Moq

Moq


1 My Cases

        1.1 简单入门

2 Reference

        2.1 Methods

        2.2 Matching Arguments

        2.3 Properties

        2.4 Events

        2.5 Callbacks

        2.6 Verification

        2.7 Customizing Mock Behavior

        2.8 Miscellaneous

        2.9 Advanced Features

        2.10 LINQ to Mocks

3 FAQ

        3.1 static class/method


1 My Cases

1.1 简单入门

// 假定我有一个 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; 

2 Reference

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!).

2.1 Methods

Methods Mock

using Moq;

// Assumptions:

public interface IFoo { Bar Bar { get; set; } string Name { get; set; } int Value { get; set; } bool DoSomething(string value); bool DoSomething(int number, string value); string DoSomethingStringy(string value); bool TryParse(string value, out string outputValue); bool Submit(ref Bar bar); int GetCount(); bool Add(int value); } public class Bar { public virtual Baz Baz { get; set; } public virtual bool Submit() { return false; } } public class Baz { public virtual string Name { get; set; } } var mock = new Mock<IFoo>(); mock.Setup(foo => foo.DoSomething("ping")).Returns(true); // out arguments var outString = "ack"; // TryParse will return true, and the out argument will return "ack", lazy evaluated mock.Setup(foo => foo.TryParse("ping", out outString)).Returns(true); // ref arguments var instance = new Bar(); // Only matches if the ref argument to the invocation is the same instance mock.Setup(foo => foo.Submit(ref instance)).Returns(true); // access invocation arguments when returning a value mock.Setup(x => x.DoSomethingStringy(It.IsAny<string>())) .Returns((string s) => s.ToLower()); // Multiple parameters overloads available // throwing when invoked with specific parameters mock.Setup(foo => foo.DoSomething("reset")).Throws<InvalidOperationException>(); mock.Setup(foo => foo.DoSomething("")).Throws(new ArgumentException("command")); // lazy evaluating return value var count = 1; mock.Setup(foo => foo.GetCount()).Returns(() => count); // returning different values on each invocation var mock = new Mock<IFoo>(); var
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值