用于单元测试的虚拟 ObjectList 生成器 [英] Dummy ObjectList generator for unit testing

查看:30
本文介绍了用于单元测试的虚拟 ObjectList 生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

你可以试试 NBuilder.其目的是快速生成测试对象.<​​/p>

如果你有 Employee 类:

公共类员工{公共字符串名称{获取;放;}公共日期时间生日{得到;放;}}

生成 10 个 Employee 对象的列表很简单:

var Employees = Builder.CreateListOfSize(10).Build();

它将为对象的所有字段生成唯一的增量值:

Name1 7/12/2012姓名 2 7/13/2012名称3 7/14/2012...

NBuilder 也有很好的强大的流畅界面,它允许为任何生成的对象设置自定义值:

var Employees = Builder.CreateListOfSize(10).TheFirst(1).With(e => e.Name = "Sergey").All().With(e => e.Address = Builder
.CreateNew().Build()).建造();

你也可以看看:

Can anyone inform whether there is any good framework in c# that will generate dummy objects and lists so that we don't need to generate the stub data manually?

解决方案

You can try NBuilder. It's purpose is rapid generation of test objects.

If you have Employee class:

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

Generating list of 10 Employee object is simple like this:

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:

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:

这篇关于用于单元测试的虚拟 ObjectList 生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆