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

查看:123
本文介绍了伪造一个没有db的活动记录模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我必须找到一些想念的东西。我正在写一个红宝石宝石,它允许与活动记录交互作为其主要功能的补充。

在为它编写测试用例时,我需要能够指定虚拟活动记录模型来测试此功能。如果我能得到一个不需要任何连接到数据库的活动记录模型的实例,这可能是非常棒的,它可能有关系,所有的东西,但不需要我在数据库中设置表。我对测试还比较陌生,在rails测试之外,我很兴奋,但似乎我应该可以很容易地做这样的事情,但我没有找到任何东西。



任何人都可以告诉我我错过了什么吗?我看过工厂,制造商,装置,所有这些似乎都想要击中分贝。人们如何测试你需要AR对象进行测试的宝石?

解析方案

听起来你需要 NullDB


NullDB是Null Object模式应用于ActiveRecord数据库适配器。它是将数据库交互转换为空操作的数据库后端。使用NullDB使您能够测试模型业务逻辑(包括after_save钩子),而无需触及真正的数据库。



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?

解决方案

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.

这篇关于伪造一个没有db的活动记录模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆