如何模拟或存根 .netTiers 生成的 DataRepository 对象
我正在使用 Rhino 模拟来模拟对象的单元测试。 我们的 DAL 使用 codesmith 从 .netTiers 模板生成代码,该模板创建这些 DataRepository 类,其中包含与数据源进行 CRUD 类型事务的所有方法。 在单元测试中,我试图模拟这个数据存储库对象,它没有接口类可用作存根。
简而言之,是否有人成功地使用 Rhino 模拟来模拟 .netTiers 生成的 DataRepository,从而避免了对测试数据库和针对最终需要拆除的数据源的实际事务的需要?
I am using Rhino mocks for unit test mocking of objects. Our DAL uses codesmith to generate code from .netTiers templates, which creates these DataRepository classes that contain all the methods for CRUD type transaction to the datasource. In unit testing, I am trying to mock this data repository object which has no interface class to use as a stub.
In Brief, has anyone successfully used Rhino mocks to mock a .netTiers generated DataRepository, avoiding the need for a test database and real transactions against the datasource that needs to be tore down at the end??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经思考这个问题有一段时间了,因为还没有人为 netTiers 编写一个模拟 DataRepository 实现(据我所知)。
由于我不太关心 TypeMock 和 RhinoMocks 的记录重放步骤,因此我选择了较新的 Moq,它将愉快地模拟 DataRepository 类或服务层以最小的麻烦调用自身。
假设您使用的是 .NET 3.5,我会推荐它。
I've been pondering this one for a while, as no one has stepped up and written a mock DataRepository implementation for netTiers yet (to my knowledge).
As I don't care too much for the record-replay steps of TypeMock and RhinoMocks, I've opted for the newer Moq, which will happily mock either the DataRepository classes or the Service layer calls themselves with minimal hassle.
Assuming you're on .NET 3.5, I'd recommend it.
简而言之,Rhino 只能模拟接口或非密封类的类型。 然后,您只能存根虚拟或抽象方法。
如果您的目标是用模拟实现替代 DataRepository,我认为您将不得不研究更高级的模拟框架,例如 TypeMock。
如果您可以控制 CS 模板,另一个选择是使用模板来生成模拟 DataRepository 实现。
In short, Rhino can only mock types that are either interfaces or non-sealed classes. And then, you can only stub methods that are virtual or abstract.
If your goal is to substitute a DataRepository with a mock implementation I think you will have to look into more advanced mocking frameworks like TypeMock.
If you're in control of the CS templates, another option would be to use the templates to also generate mock DataRepository implementations.