测试:

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. Nuxt.js 应用中的 imports:dirs 事件钩子详解

    title: Nuxt.js 应用中的 imports:dirs 事件钩子详解 date: 2024/10/30 updated: 2024/10/30 author: cmdragon excerp ...

  2. 多模型COE方法

    1.概述 在当前的人工智能发展中,单一模型的表现往往难以满足复杂任务的需求.为应对这些挑战,多模型协作的方法应运而生,"专家组合"(Mixture of Experts)便是其中一 ...

  3. Redis的ZSet底层数据结构,ZSet类型全面解析

    文章目录 一.ZSet有序集合类型 1.1 简介 1.2 应用场景 1.3 底层结构 1.4 ZSet常用命令 二.ZSet底层结构详解 2.1 数据结构 2.2 压缩列表ZipList 2.3 跳表 ...

  4. Postman 免登录测试后端接口

    今天跟测试同学学习了下用Postman免登录测试后端接口,测试同学除了会对我们系统前端测试外,一些后端接口涉及危险操作也会使用Postman 对接口进行测试,这个时候就需要解决一个接口免登录的问题,他 ...

  5. 鸿蒙接入Flutter3.22

    配置环境变量 配置HarmonyOS SDK和环境变量 API12, deveco-studio-5.0 或 command-line-tools-5.0 配置 Java17 配置环境变量 (SDK, ...

  6. glibc 内存分配与释放机制详解

    作者:来自 vivo 互联网存储团队- Wang Yuzhi 本文以一次线上故障为基础介绍了使用 glibc 进行内存管理可能碰到问题,进而对库中内存分配与释放机制进行分析,最后提供了相应问题的解决方 ...

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

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

  8. AI智能学生体测小程序解决方案

    引言: 近年来,随着教育理念的提升,对学生综合素质的教育越发重视,特别是越发重视学生的身体素质提升,各阶段的升学考试也将体测纳入考核范围.学校也推出了各种体测锻炼促进手段,今天为您介绍一个基于小程序的 ...

  9. 深入Log4J源码之Log4J Core

    毕业又赶上本科的同学会,还去骑车环了趟崇明岛,六月貌似就没消停过,不过终于这些事情基本上都结束了,我也可以好好的看些书.读些源码.写点博客了. Log4J将写日志功能抽象成七个核心类/接口:Logge ...

  10. HTML5 拖拽接口

    1.首先,为了使元素可拖动,要先设置元素为可拖拽 方法:添加draggable属性,设置为 true 注意:链接和图像默认就支持拖拽,另外,如果一个元素中的文本被选中,那么这个元素和他的文本节点此时都 ...