我觉得自己错过了一些东西。 我正在写一个红宝石宝石,它允许与活动记录交互作为其主要功能的补充。
在为它编写测试用例时,我需要能够指定虚拟活动记录模型来测试此功能。 如果我能得到一个不需要任何连接到数据库的活动记录模型的实例,这可能是非常棒的,它可能有关系,所有的东西,但不需要我在数据库中设置表。 我对测试相当陌生,在rails测试之外,我非常兴奋,但似乎我应该可以很容易地做这样的事情,但我没有找到任何东西。
任何人都可以告诉我我错过了什么吗? 我看过工厂,制造商,装置,所有这些似乎都想要击中分贝。 人们如何在需要AR对象测试的地方测试宝石?
I feel like I've got to me missing something. I'm writing a ruby gem that allows interaction with active record as an add on to its primary function.
In writing test cases for it, I need to be able to specify dummy active record models to test this functionality. It would be superb if I could get an instance of an active record model that didn't need any connection to a db, that could have relations, all that stuff, but didn't require me to setup tables in a database. I'm fairly new to testing, and outside of rails testing i'm pretty dang new, but it seems like I should be able to do something like that fairly easily, but I'm not finding anything.
Can anyone tell me what I'm missing? I've looked at factories, fabricators, fixtures, all those seem to want to hit the db. How do people test gems where you need AR object only for testing?
最满意答案
这听起来像你需要NullDB :
NullDB是应用于ActiveRecord数据库适配器的空对象模式。 它是将数据库交互转换为空操作的数据库后端。 使用NullDB使您能够测试模型业务逻辑 - 包括after_save挂钩 - 而无需触及真正的数据库。
It sounds like you need NullDB:
NullDB is the Null Object pattern as applied to ActiveRecord database adapters. It is a database backend that translates database interactions into no-ops. Using NullDB enables you to test your model business logic - including after_save hooks - without ever touching a real database.
更多推荐
发布评论