伪造一个没有db的活动记录模型(Fake an active record model without db)

编程入门 行业动态 更新时间:2024-10-25 16:30:27
伪造一个没有db的活动记录模型(Fake an active record model without db)

我觉得自己错过了一些东西。 我正在写一个红宝石宝石,它允许与活动记录交互作为其主要功能的补充。

在为它编写测试用例时,我需要能够指定虚拟活动记录模型来测试此功能。 如果我能得到一个不需要任何连接到数据库的活动记录模型的实例,这可能是非常棒的,它可能有关系,所有的东西,但不需要我在数据库中设置表。 我对测试相当陌生,在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.

更多推荐

本文发布于:2023-07-30 21:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340312.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   Fake   db   model   record

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!