测试:

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的更多相关文章

  1. 开源项目Foq简介

        Foq是一个轻量级-线程安全的mocking类库.使用它来mock抽象类与接口这是我们通常的做法.Foq的名字来自Moq,如果你使用过Moq的话,自然后联想到它能做什么.Foq主要是为了F#的 ...

  2. C#7的9个新语法

    一.out变量 在c#7之前我们得这样 在c#7中我们可以这样 当然你还可以使用"var" 这算一个小更新,其实这个问题存在很久了,应该也很好解决,不知为何到c#7才开始引入,不管 ...

  3. mshadow的原理--MXNet

    mshadow的原理--MXNet 这文章主要解释了表达式模板的工作原理(也是mshadow的主要原理),文章的前半部分是翻译自exp-template/README.md.我们会解释它为什么会影响编 ...

  4. C#7

    C#7 阅读目录 out变量 元组(Tuples) 模式匹配(Pattern matching) 本地引用和返回(Ref locals and returns) 本地函数(Local function ...

  5. Cyber Security - Palo Alto Firewall Interface Types

    Multiple options to integrate the Palo Alto Firewall into your: Network Layer 2 interfaces and VLAN ...

  6. MShadow中的表达式模板

    表达式模板是Eigen.GSL和boost.uBLAS等高性能C++矩阵库的核心技术.本文基于MXNet给出的教程文档来阐述MXNet所依赖的高性能矩阵库MShadow背后的原理. 编写高效的机器学习 ...

  7. c#单元测试:使用Moq框架Mock对象

    在.net中有几种mock框架可供选择,比如NMock,PhinoMocks,FakeItEasy和Moq.尽管Moq相对较新,但是它非常易用.不需要像传统的Record/Replay.并且使用Moq ...

  8. 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 ...

  9. 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 ...

  10. Moq基础

    一.概念 Moq是利用诸如Linq表达式树和Lambda表达式等·NET 3.5的特性,为·NET设计和开发的Mocking库.Mock字面意思即模拟,模拟对象的行为已达到欺骗目标(待测试对象)的效果 ...

随机推荐

  1. 为什么我越来越喜欢用DDD — DDD架构篇(1)

    Hello DDD DDD 是一种软件设计方法,DDD 是指导我们做软件工程设计的一种手段.它提供了用切割工程模型的各类技巧,如:领域.界限上下文.实体.值对象.聚合.工厂.仓储等.通过 DDD 的指 ...

  2. 什么是AOP面向切面编程?怎么简单理解?

    本文原文地址:什么是AOP面向切面编程?怎么简单理解? 什么是AOP面向切面编程 面向切面编程(AOP)通过将横切关注点(cross-cutting concerns)分离出来,提供了一种增强代码模块 ...

  3. 研发LLM模型,如何用数值表示人类自然语言?

    上一篇:<人工智能--自然语言处理简介> 序言:人工智能大语言模型(LLM)如何理解人类的自然语言?这个过程的核心在于将文本转化为计算机能处理的数值形式,经过计算,最终达到对语言的理解.起 ...

  4. 基于Java+SpringBoot+Mysql实现的古诗词平台功能设计与实现九

    一.前言介绍: 1.1 项目摘要 随着信息技术的迅猛发展和数字化时代的到来,传统文化与现代科技的融合已成为一种趋势.古诗词作为中华民族的文化瑰宝,具有深厚的历史底蕴和独特的艺术魅力.然而,在现代社会中 ...

  5. IDEA方法模板、类模板设置记录

    类模板 设置入口 Mac:common+,进入设置->Editor->File and Code Templates->Includes 模版代码 /** * todo * * @a ...

  6. P9150 邮箱题

    P9150 邮箱题 Alex_Wei 做法妙. 思路 首先我们可以建出两张图,一张是按照题目的要求形成的有向图,一张是由有向边 \((i,k_i)\) 形成的钥匙图. 在钥匙图中,每个点有且仅有一入度 ...

  7. 浅析JVM内存结构和6大区域

    内存作为系统中重要的资源,对于系统稳定运行和高效运行起到了关键的作用,Java和C之类的语言不同,不需要开发人员来分配内存和回收内存,而是由JVM来管理对象内存的分配以及对象内存的回收(又称为垃圾回收 ...

  8. 使用 Antlr 开发领域语言

    高 尚 (gaoshang1999@163.com), 软件工程师, 中国农业银行软件开发中心 简介: Antlr 是一个基于 Java 开发的功能强大的语言识别工具,Antlr 以其简介的语法和高速 ...

  9. 从零打造基础HTTP服务器:揭秘背后的技术魔法-MiniTomcat

    <从零打造基础HTTP服务器:揭秘背后的技术魔法-MiniTomcat> 嘿,各位技术发烧友们!今天咱们要一起踏上一段超级刺激的技术之旅,去揭开从零实现一个基础HTTP服务器的神秘面纱.这 ...

  10. OSG开发笔记(三十六):osg3.4.0基于windows平台msvc2017x64编译器编译并移植Demo

    前言   本篇编译osg3.4.0的msvc2017x64版本,之前使用的都是mingw32版本.   OSG编译 步骤一:下载解压   下载3.4.0版本.   步骤二:使用cmake配置      ...