简体   繁体   English

用于单元测试的虚拟ObjectList生成器

[英]Dummy ObjectList generator for unit testing

任何人都可以告知c#中是否有任何好的框架会生成虚拟对象和列表,这样我们就不需要手动生成存根数据了?

You can try NBuilder . 你可以尝试NBuilder It's purpose is rapid generation of test objects. 它的目的是快速生成测试对象。

If you have Employee class: 如果您有Employee类:

public class Employee
{
    public string Name { get; set; }
    public DateTime Birthday { get; set; }
} 

Generating list of 10 Employee object is simple like this: 生成10个Employee对象的列表很简单,如下所示:

var employees = Builder<Employee>.CreateListOfSize(10).Build();

It will generate unique incremental values for all fields of object: 它将为对象的所有字段生成唯一的增量值:

Name1 7/12/2012
Name2 7/13/2012
Name3 7/14/2012
...

Also NBuilder has nice powerful fluent interface, which allows to setup custom values for any generated object: 此外,NBuilder具有强大的流畅界面,允许为任何生成的对象设置自定义值:

var employees = Builder<Employee>.CreateListOfSize(10)
    .TheFirst(1).With(e => e.Name = "Sergey")
    .All().With(e => e.Address = Builder<Address>.CreateNew().Build())
    .Build();

Also you can take a look at: 你也可以看看:

The framework I like to use and does what you ask is the following: Rhino Mocks 我喜欢使用的框架,并按照以下要求执行: Rhino Mocks

This is for C#, and its superior. 这适用于C#,它的优越性。

The framework I like to use and does what you ask is the following: Unitils 我喜欢使用的框架并按照您的要求执行以下操作: Unitils

but then again, I don't know for which programming language you are asking ? 但话说回来,我不知道你问的编程语言是什么? The one proposed is for Java. 提议的是Java。

If you need to create fake implementations of interfaces or abstract classes then there's a bunch of "mocking frameworks" available. 如果你需要创建接口或抽象类的伪实现,那么就有一堆“模拟框架”可用。 One of them is Rhimo Mocks mentioned by Harry. 其中一个是哈利提到的Rhimo Mocks。 For you as a beginner in this area I'd also suggest Moq as it's more straightforward compared to Rhino Mocks (IMHO). 对于你作为这个领域的初学者,我也建议Moq,因为它比Rhino Mocks(恕我直言)更直接。

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

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