简体   繁体   English

如何在MOQ中自动模拟容器(例如IList)而不使用extensions / contrib

[英]How to auto-mock a container (e.g. IList) in MOQ without extensions/contrib

i wonder if it is possible to auto mock a container in MOQ without any additions to the MOQ lib. 我想知道是否有可能在MOQ中自动模拟容器而不添加任何MOQ库。 I am having problems finding a clean way to automock an IList. 我在找到一个自动插入IList的干净方法时遇到了问题。

Thanks in advance! 提前致谢!

Answer to your question: No. 回答你的问题:不。

Do you really need to mock IList? 你真的需要模仿IList吗?

Mocks are typically used to: 模拟通常用于:

  • To test behaviour (via expectations) rather than results. 测试行为(通过期望)而不是结果。
  • To abstract away complex or heavy dependencies. 抽象出复杂或重的依赖。
  • To simplify your tests code by easily returning a desired value. 通过轻松返回所需的值来简化测试代码。
  • To test only your class under tests. 仅测试您的课程。

You could for example mock a repository that access a database. 例如,您可以模拟访问数据库的存储库。 Normally your tests would not mock a list but rather have a mocked object return a list with the data that you need for your test. 通常,您的测试不会模拟列表,而是使用模拟对象返回包含测试所需数据的列表。

ie: 即:

var aList = new List<int>() { 1, 2, 3, 4, 5 };
var mockService = new Mock<IMyService>();
mockService.Setup(mock => mock.GetFooList()).Returns(aList);

It might help clarify your question if you specify why you need to mock a container. 如果您指定需要模拟容器的原因,这可能有助于澄清您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM