linq和lambda

Kzu and friends have a new pet project called Moq, which may be the coolest derivative open source project name ever. But, before I get into that...

Kzu和朋友有一个名为Moq的新宠物项目,这可能是有史以来最酷的衍生开源项目名称。 但是,在我开始之前...

There's lots of interesting Mock Object Frameworks out there. The ones that you'll always hear about (because they are awesome) are Rhino.Mocks and TypeMock (Full Disclosure: we used TypeMock at Corillian, my last job. Here's a Case Study that was done.)

有很多有趣的Mock对象框架。 您会经常听到的(因为它们很棒)是Rhino.Mocks和TypeMock (完整披露:我们在上次工作的Corillian使用TypeMock。这是完成的一个案例研究 )

Both frameworks are very powerful. Here's Phil Haack's post on mocking IHttpRequest and IHttpReponse in the new MVC Framework using Rhino Mocks. Here's Travis Illig using TypeMock to mock the actual HttpContext (not the MVC interface) earlier this year.

这两个框架都非常强大。 这是Phil Haack在使用Rhino Mocks的新MVC框架中模拟IHttpRequest和IHttpReponse的文章。 这是Travis Illig在今年早些时候使用TypeMock模拟实际的HttpContext(不是MVC接口)的情况。

One of the things that often comes up when comparing Mock Frameworks, after their core capabilities, is their syntax.

在比较Mock框架的核心功能之后,经常会想到的一件事就是它们的语法。

Early Mocking frameworks like NMock (which isn't really used much anymore) mock interfaces and use a quasi-fluent interface that breaks down when you start referring to methods and properties using strings. Why is this bad? Well, for one it means when you refactor using tools like CodeRush or Resharper the system doesn't realize that a string referring to "Foo" means the method x.Foo(). For example, here's a snippet from NMock:

早期的Mocking框架(例如NMock) (不再使用了)模拟了接口,并使用了准流利的接口,当您开始使用字符串引用方法和属性时,该接口会崩溃。 为什么这样不好? 好吧,对于这意味着,当您使用诸如CodeRush或Resharper之类的工具进行重构时,系统不会意识到引用“ Foo”的字符串表示方法x.Foo()。 例如,这是NMock的片段:

   mocks = new Mockery();ITransferFundsView mockView = mocks.NewMock<ITransferFundsView>();Expect.Once.On(mockView).GetProperty("Amount").Will(Return.Value(200.00));

You get the idea. We're referring to a property "FromAccount" via a string passed into GetProperty, rather than in a strongly typed way.

你明白了。 我们通过传递给GetProperty的字符串而不是强类型方式来引用属性“ FromAccount”。

RhinoMock is smarter, and might look like this snippet:

RhinoMock更加智能,可能看起来像以下片段:

     mockView = (ITransferFundsView)mocks.CreateType(typeof(ITransferFundsView)); 

     SetupResult.For(mockView.Amount).Return(200.00);

See how much clearer it is to just call the property?

看到仅调用该属性要清楚多少?

TypeMock is implemented as a profiler under the covers so it can do some pretty powerful stuff like "recording" your expectations do you can Tivo them back. So with TypeMock you'd do this using their "Natural TypeMock" syntax.:

TypeMock是作为幕后分析器实现的,因此它可以执行一些非常强大的工作,例如“记录”您的期望,也可以将其翻转。 因此,使用TypeMock,您可以使用其“ Natural TypeMock”语法进行此操作。

 using (RecordExpectations recorder = RecorderManager.StartRecording()) {    double foo = mockView.Ammount;    recorder.Return(200.00); } //the next time you call mockView.Ammount, it will return 200.00.

Either way, you can see why both RhinoMocks and TypeMock's syntaxs would be refactoring tool friendly. They compile against the real method signatures and properties so they can been "seen" by the tool. As for which one you like, that's a religious argument I won't get into, but depending on what you need, both are fine choices and they each have a very friendly syntax.

无论哪种方式,您都可以看到为什么RhinoMocks和TypeMock的语法都将重构工具友好。 它们根据实际的方法签名和属性进行编译,因此可以通过工具“看到”它们。 至于您喜欢哪一种,我不会讲这是一种宗教论据,但是根据您的需要,两者都是不错的选择,并且每种语法都非常友好。

Why all this background? There's a new Mocking Framework in town, and it's C# 3.0 specific using LINQ Expression Trees and Lambda Expressions.

为什么所有这些背景? 镇上有一个新的Mocking框架,它使用LINQ表达式树和Lambda表达式特定于C#3.0。

The same snippet in Moq might look like this:

Moq中的相同代码段可能如下所示:

var mockView = new Mock<ITransferFundsView>;mockView.Expect(x => x.Amount).Returns(200.00);

Note the use of Generics for the Type ITransferFundsView and the interesting use of a Lambda expression to indicate a mocked call to the Amount property. Check out the Moq QuickStart for more interesting examples.

注意类型ITransferFundsView使用泛型,以及有趣的使用Lambda表达式来指示对Amount属性的模拟调用。 请查看Moq快速入门,以获取更多有趣的示例。

Here's another deeper, more interesting example that shows how lambdas might really be a great feature for any .NET Mock Framework. It's the "It" class combined with the power of predicates.

这是另一个更深入,更有趣的示例,该示例说明了lambda如何真正成为任何.NET Mock Framework的强大功能。 这是“ It”类,结合谓词的功能。

The It class provides for a kind of wildcard support. There's an IsAny, IsInRange and IsRegex. The coolest one though is the plain It.Is method that receives a predicate!

It类提供了一种通配符支持。 有一个IsAnyIsInRangeIsRegex 不过,最酷的是接收谓词的普通It.Is方法!

mock.Expect(x => x.DoInt(It.Is<int>(i => i % 2 == 0))).Returns(1);
 mock.Expect(x => x.DoInt(It.Is<int>(i => i % 2 == 0))).Returns(1);

Here the mocked object will return the value 1 only if the integer passed to the DoInt method is an even number.

在此,仅当传递给DoInt方法的整数为偶数时,模拟对象才会返回值1。

The cool thing about this approach is that you don't need the mocking library to provide all the filters you need, as you can simply pass a predicate for any condition you can dream of.

这种方法的妙处在于,您不需要模拟库来提供所需的所有过滤器,因为您可以简单地为您可以梦想的任何条件传递谓词。

Slick. Moq is definitely a project to watch.

光滑最小起订量绝对是一个值得关注的项目。

翻译自: https://www.hanselman.com/blog/moq-linq-lambdas-and-predicates-applied-to-mock-objects

linq和lambda

linq和lambda_最小起订量:应用于模拟对象的Linq,Lambda和谓词相关推荐

  1. ECSHOP模板堂商品最小起订量插件

    WBB ECshop二次开发博客 Ecshop 最小起订量如何设置 第一步,商品表必须有个字段  代表某个商品 最小订购数量->min_number     打开goods表   在最后字段添加 ...

  2. Ecshop 最小起订量如何设置

    第一步,商品表必须有个字段  代表某个商品 最小订购数量->min_number 打开goods表   在最后字段添加一个min_number  tinyint类型 默认值为0  代表没有最小起 ...

  3. ECSHOP最小起订数插件,ECSHOP商品起订数量插件,ECSHOP商品批发限购插件,ecshop商品批发起订量限购插件,ecshop商品购买批发最小起订量数量插件

    ECSHOP插件:ecshop批发商起订量限制插件达到订购量起批. 对自己下面的分销商批发订购商品数量做限制,减少工作量,拒绝无效单.可以在后台单独设置或者批量设置商品的最低订购量.方便供应商管理商品 ...

  4. 计数器按照,商品起订量和最小包装量,选择步数和校验

    eg:商品起订量:1,最小包装量:20 根据最小包装量选择步数    :step="goodSkuList.miniPachingQuantity" 根据起订量默认最小值     ...

  5. .NET 3.5(11) - DLINQ(LINQ to SQL)之大数据量分页、延迟执行和日志记录

    步步为营VS 2008 + .NET 3.5(11) - DLINQ(LINQ to SQL)之大数据量分页.延迟执行和日志记录 作者:webabcd 介绍 以Northwind为示例数据库,DLIN ...

  6. [深入学习C#]LINQ查询表达式详解(1)——基本语法、使用扩展方法和Lambda表达式简化LINQ查询

    此文章非原创,转载自诗人江湖老,原文地址 在Git上下载源码 在工程中我们少不了要定义类或者结构去储存数据,这些数据将被临时地储存在内存中,现在我们想要对其完成一些类似于查找.过滤等等常见的任务的时候 ...

  7. 飞机订票系统的模拟(C语言实现)

    问题描述与题目要求 问题描述: 假定某民航有M个航次的班机,每个航次都只到达一个地方.试为该机场售票处设计一个自动订票和退票系统,要求系统具有以下功能: (1) 订票:若该航次余票大于等于乘客订票数, ...

  8. 三菱d700变频器模拟量控制_PLC和变频器:开关量控制和模拟量控制什么区别?...

    提起PLC和变频器,相信很多的电工老师傅都非常了解,甚至于每天都在接触PLC和变频器,老师傅们都知道开关量控制和模拟量控制是PLC和变频器经常使用的2种控制方式,都应用非常广泛. 但是对于一些刚入门学 ...

  9. 无线开关量收发模块、模拟量无线收发模块、无线液位采集传输控制系统、无线压力传感器、云平台远程监控、本地监控、无线西门子plc在污水处理方案中的应用

    ​​​​​ 无线开关量收发模块.模拟量无线收发模块.无线液位采集传输控制系统.无线压力传感器.云平台远程监控.本地监控.无线西门子plc在污水处理方案中的应用 项目背景 污水处理是指为使污水达到排入某 ...

最新文章

  1. 《Redis 系列》- list命令
  2. 为什么加了@Transactional注解,事务没有回滚?
  3. matlab 定义一个有自变量的方程_Eviews、Stata、Python、Matlab、R描述+相关+回归分析教程汇总...
  4. iOS开发的学习笔记
  5. OFD文件结构--Pages~Page_0~Content.xml
  6. 面向抽象编程(模拟Spring的简单实现)
  7. IIS7的CMD指令
  8. 如何获取EasyCVR平台设备通道的RTMP视频流地址?
  9. 单片机实验报告太原理工大学_太原理工大学单片机实验报告
  10. STL库中常用的数据结构
  11. 电子式电能表试行检定规程
  12. 微信小程序的AppID在哪?
  13. IE浏览器被2345网址导航劫持
  14. 【ORA-RAC】ORA-15045: ASM file name '+DATA01' is not in reference form
  15. 苹果或将采用高通屏下指纹方案,5GiPhone基带由三星、高通共同提供...
  16. 深度剖析:PS中的3大类调色功能。
  17. Android studio 写xml的不能自动补全的问题
  18. 多个操作语句的触发器为什么在执行时,只执行了第一句?
  19. 毕业一年的组长,刚去了阿里做Devops。年薪40W的offer
  20. torch+cuda gpu并行计算

热门文章

  1. 避坑14_此浏览器或应用可能不安全。了解详情请尝试使用其他浏览器。
  2. ubuntu修改用户名和home对应的目录名
  3. UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xbb in position 0: invalid start byte
  4. MIMIC-CXR数据集的下载
  5. 核函数在SVM的应用,核函数到底是什么
  6. 硬件工程师都没人干了_我的汽车工程师之路
  7. VB.NET中IIF和IF使用效率分析
  8. 交叉线、直通线、反转线的区别与应用
  9. Android必知必会-Stetho调试工具
  10. 三本计算机的专业需要考研嘛,三本计算机考研难吗