Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 使用T4为接口编写gen模拟对象代码是一个好主意吗?_.net_Testing_Mocking - Fatal编程技术网

.net 使用T4为接口编写gen模拟对象代码是一个好主意吗?

.net 使用T4为接口编写gen模拟对象代码是一个好主意吗?,.net,testing,mocking,.net,Testing,Mocking,我熟悉单元测试,但仍在学习模拟和模拟框架。我明白我的想法,但语法似乎还是有点陌生。我正在考虑创建一些T4模板,自动生成接口和类的模拟实现。结合使用泛型和扩展方法,我可以这样做: var service = new MockCustomerService(); //code-generated and implements ICustomerService, has a method named 'Insert' var target = new CustomerController(servic

我熟悉单元测试,但仍在学习模拟和模拟框架。我明白我的想法,但语法似乎还是有点陌生。我正在考虑创建一些T4模板,自动生成接口和类的模拟实现。结合使用泛型和扩展方法,我可以这样做:

var service = new MockCustomerService(); //code-generated and implements ICustomerService, has a method named 'Insert'
var target = new CustomerController(service);
var item = new Customer();
target.Insert(item);
service.InsertMethod.Assert.WasLastCalledWith(item);
或:

首先,这真的是在嘲弄我,还是我遗漏了一些基本的东西。第二,这似乎是一种合理的方法,还是除了不必手动创建类似的类之外,还需要使用框架?第三,还有其他类似的东西吗?我环顾四周,没有发现太多

首先,这真的是在嘲笑吗, 还是我遗漏了一些基本的东西

您展示的第二个示例中,有一个模拟发生在这一行:

target.GetByIdMethod.ShouldReturn(item);
这使您能够为target.GetByIdMethod项返回假值,而不实际调用target.GetByIdMethod

第二,这看起来会吗 要有一个合理的方法,还是有 使用超越框架的原因 不必手动创建类似的 上课

我们使用mocking框架的原因是,我们希望为某些方法返回假值,以便我们可以轻松控制要测试的内容。此外,通过使用模拟框架,我们还可以断言调用了正确的方法,和/或使用了正确的参数

第三,还有其他问题吗 有类似的吗


你什么意思?你是说有没有类似的模仿框架?或者有没有单元测试代码模板生成引擎?有很多。至于是否有单元测试代码模板生成引擎,我不确定

谢谢你的回答。明确地说,我所说的代码生成是为了生成实际的具体模拟对象,而不是生成单元测试。换句话说,我认为T4生成MockCustomerService可能比在运行时使用RhinoMocks、Moq等工具生成它要好。
target.GetByIdMethod.ShouldReturn(item);