对对象的所有公共属性生成断言语句
在我当前的项目中,我们希望通过一组集成测试来测试从 sql 到数据模型的映射。
为此,我们基本上需要断言对象上的所有公共属性。
有没有办法自动为对象上的所有公共属性生成断言语句?设置期望值将是我们的工作,但编写大量断言语句的费力工作将是计算机可以完成的事情。
我正在考虑使用 T4 模板或者利用 resharper 的强大功能?
In my current project we want to test our mapping from sql to datamodels through a set of integration tests.
For this we need to basically assert all public properties on an object.
Is there a way to automatically generate assert statements for all public properties on an object? Setting the expected values would be our job, but the laborious job of writing the numerous assert statements would be something a computer could do.
I'm thinking of using a T4 template or maybe harness the awesomeness of resharper?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Visual Studio 的测试生成:
What you could use is the test generation of Visual Studio:
您可以通过创建满足您需要的插件来利用 ReSharper 的强大功能。 本指南将帮助您入门。
You can harness the awesomeness of ReSharper by creating a plug-in that does what you need. This guide will get you started.
T4 听起来也是一个简单省力的选择。做起来也很有趣,而且文档不多(不像制作 Resharper 插件)。请务必将 T4 模板放在不同的程序集中,以便可以使用
@ assembly
指令来引用要为其生成测试的程序集。然后,从 T4 模板内部使用反射来循环访问类的属性。或者类似的东西。确保使用完整路径引用程序集(或将其放入 GAC 中)。
我推荐 tangible T4 editor< /a> (不需要捆绑的“建模工具”)一些不错的语法突出显示和智能感知。
T4 sounds like a simple low-effort option, too. Also fun to do, and not very documentation-heavy (unlike e.g. making a Resharper plugin). Be sure to put the T4 template in a different assembly, so that you can use the
@assembly
directive to refer to the assembly that you're generating tests for. Then, use reflection right from inside the T4 template to loop through the class's properties. Or something like that.Make sure you refer to the assembly with a full path (or put it in the GAC).
I'd recommend the tangible T4 editor (no need for the bundled "modeling tools") for some decent syntax highlighting and intellisense.