C# Moq - Non-overridable members may not be used in setup / verification expressions
测试:
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Mock<TestClass> moc = new Mock<TestClass>();
moc.Setup(x => x.GetItem(It.IsAny<int>())).Returns("123");
moc.Setup(x => x.name).Returns("wgscd");
TestClass t =moc.Object;
Assert.AreEqual("123", t.GetItem(222));
Assert.AreEqual("wgscd", t.name); }
} public class TestClass
{
public string GetItem(int i) {
return "";
}
public string name { get;set; } }
上面代码会错误出现:
Moq - Non-overridable members may not be used in setup / verification expressions
修改后OK:
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Mock<TestClass> moc = new Mock<TestClass>();
moc.Setup(x => x.GetItem(It.IsAny<int>())).Returns("123");
moc.Setup(x => x.name).Returns("wgscd");
TestClass t =moc.Object;
Assert.AreEqual("123", t.GetItem(222));
Assert.AreEqual("wgscd", t.name); }
} public class TestClass
{
public virtual string GetItem(int i) {
return "";
}
public virtual string name { get;set; } }
原因是:moc 的对象的成员或字段必须可以 override 的。
Moq creates an implementation of the mocked type. If the type is an interface, it creates a class that implements the interface. If the type is a class, it creates an inherited class, and the members of that inherited class call the base class. But in order to do that it has to override the members. If a class has members that can't be overridden (they aren't virtual, abstract) then Moq can't override them to add its own behaviors.
In this case there's no need to mock PagingOptions because it's easy to use a real one. Instead of this:
var mockPagingOptions = new Mock<PagingOptions>();
mockPagingOptions.Setup(po => po.Limit).Returns(25);
mockPagingOptions.Setup(po => po.Offset).Returns(0);
Do this:
var pagingOptions = new PagingOptions { Limit = 25, Offset = 0 };
How do we determine whether or not to mock something? Generally speaking, we mock something if we don't want to include the concrete runtime implementation in our test. We want to test one class not both at the same time.
But in this case PagingOptions is just a class that holds some data. There's really no point in mocking it. It's just as easy to use the real thing.
另外有个回答的还提到测试私有字段的方法(就是通过反射设置私有字段值):
n case you reached this question based on the original title Non-overridable members may not be used in setup / verification expressions and none of the other answers have helped you may want to see if reflection can satisfy your test needs.
Suppose you have a class Foo with a property defined as public int I { get; private set; }
If you try the various methods in the answers here few of them will work for this scenario. However you can use .net reflection to setup a value of an instance variable and still keep fairly good refactoring support in the code.
Here is a snippet that sets a property with a private setter:
var foo = new Foo();
var I = foo.GetType().GetProperty(nameof(Foo.I), BindFlags.Public | BindingFlag.Instance);
I.SetValue(foo, 8675309);
C# Moq - Non-overridable members may not be used in setup / verification expressions的更多相关文章
- 开源项目Foq简介
Foq是一个轻量级-线程安全的mocking类库.使用它来mock抽象类与接口这是我们通常的做法.Foq的名字来自Moq,如果你使用过Moq的话,自然后联想到它能做什么.Foq主要是为了F#的 ...
- C#7的9个新语法
一.out变量 在c#7之前我们得这样 在c#7中我们可以这样 当然你还可以使用"var" 这算一个小更新,其实这个问题存在很久了,应该也很好解决,不知为何到c#7才开始引入,不管 ...
- mshadow的原理--MXNet
mshadow的原理--MXNet 这文章主要解释了表达式模板的工作原理(也是mshadow的主要原理),文章的前半部分是翻译自exp-template/README.md.我们会解释它为什么会影响编 ...
- C#7
C#7 阅读目录 out变量 元组(Tuples) 模式匹配(Pattern matching) 本地引用和返回(Ref locals and returns) 本地函数(Local function ...
- Cyber Security - Palo Alto Firewall Interface Types
Multiple options to integrate the Palo Alto Firewall into your: Network Layer 2 interfaces and VLAN ...
- MShadow中的表达式模板
表达式模板是Eigen.GSL和boost.uBLAS等高性能C++矩阵库的核心技术.本文基于MXNet给出的教程文档来阐述MXNet所依赖的高性能矩阵库MShadow背后的原理. 编写高效的机器学习 ...
- c#单元测试:使用Moq框架Mock对象
在.net中有几种mock框架可供选择,比如NMock,PhinoMocks,FakeItEasy和Moq.尽管Moq相对较新,但是它非常易用.不需要像传统的Record/Replay.并且使用Moq ...
- 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 ...
- AutoMapper:Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 应用场景:ViewModel==>Mode映射的时候出错 AutoMappe ...
- Moq基础
一.概念 Moq是利用诸如Linq表达式树和Lambda表达式等·NET 3.5的特性,为·NET设计和开发的Mocking库.Mock字面意思即模拟,模拟对象的行为已达到欺骗目标(待测试对象)的效果 ...
随机推荐
- 使用FastAPI整合Gradio和Django
大家好,我是每天分享AI应用的萤火君! 经常接触机器学习的同学可能都接触过Gradio这个框架,Gradio是一个基于Python的专门为机器学习项目创建的快速开发框架,可以让开发者快速发布自己的模型 ...
- 遗传算法+强化学习—TPG—Emergent Tangled Graph Representations for Atari Game Playing Agents_2
最近在看进化算法在强化学习(RL)领域的一些应用,有些论文中将使用进化算法解决强化学习问题的算法归为非强化学习算法,然而又有些论文把使用进化算法解决强化学习问题的算法归为强化学习算法,不过更多的论文是 ...
- DDCA —— 大缓存、虚拟内存:多核缓存、NUCA缓存、页表等
1. 缓存中的多核问题 1.1 多核系统中的缓存 Intel Montecito缓存 两个 core,每个都有一个私有的12 MB的L3缓存和一个1 MB的L2缓存,图中深蓝色部分均为L3缓存. 在多 ...
- frida 连接夜神模拟器
adb connect 127.0.0.1:62001 adb devices adb forward tcp:27042 tcp:27042 adb forward tcp:27043 tcp:27 ...
- 在Keil中使用ST-LINK烧录STM32程序指南
前言 之前玩STM32都是用J-LINK烧录程序,不仅便捷,而且烧录的速度比用串口快好多. 最近我接了几个32单片机的毕设单子,便买了几块C8T6的最小系统板用来开发.最初我还是用J-LINK烧录C8 ...
- win10中Docker安装、构建镜像、创建容器、Vscode连接实例
Docker方便一键构建项目所需的运行环境:首先构建镜像(Image).然后镜像实例化成为容器(Container),构成项目的运行环境.最后Vscode连接容器,方便我们在本地进行开发.下面以一个简 ...
- (Python基础教程之十七)Python OrderedDict –有序字典
一个OrderedDict 维护插入顺序添加到字典中的项目.项目的顺序在迭代或序列化时也会保留. 1. Python OrderedDict示例 OrderedDict 是python collect ...
- Spring 开发 Swing GUI 简介
依赖注入和富客户机 Chad Woolley (thewoolleyman@gmail.com), 软件开发人员, Ionami 简介: 本教程介绍了 Spring 框架以及依赖注入的概念(也称为反 ...
- Winform TabControl动态添加TabPage
在Winform中,标签页是我们很难绕开的一个控件,而且,我们经常有动态添加标签页的需求. 这里介绍一个最简单的添加方法: 首先,我们把需要添加的内容做成UserControl,这样,我们就可以在添加 ...
- Limit线段树题单题解(更新中)
P3373 线段树模板 2 \(1 \leq n \leq 10^5\) 题解:考查标记与标记的合并 我们考虑打两个懒惰标记实现区间乘和区间加 线段树维护区间和 对于信息与信息的合并:左儿子加上右儿子 ...