简体   繁体   English

Rails活动记录存根与数组

[英]Rails active record stub with array

i'm looking for some gem which allows me to stub AR with in memory array. 我正在寻找一些宝石,可以让我在内存阵列中存根AR。

So, all finds will work, but real db wouldn't be touched. 因此,所有查找都将起作用,但不会影响实际的数据库。

Please advice, 请指教,

Thanks! 谢谢!

I really doubt there is one, because relational algebra won't be an easy thing to mock/stub. 我真的很怀疑这是什么,因为关系代数很难模拟/存根。

If You are looking for a way to better manage Your test data, take a look at different factory gems like factory_girl , machinist . 如果您正在寻找一种更好地管理测试数据的方法,请查看不同的工厂宝藏,例如factory_girl机械师

But I guess it's speed You are after. 但是我想这是您追求的速度。 In this case solid OO design is Your friend. 在这种情况下,可靠的OO设计是您的朋友。 You can build an abstractions that would allow You easily to mock them, because You would be calling custom methods on plain Ruby objects(not Rails ORM directly). 您可以构建一个可以轻松模拟它们的抽象,因为您将在普通的Ruby对象上(而不是直接在Rails ORM上)调用自定义方法。 To understand the concept better I suggest watching Sandi Metz presentations , but in Your particular case You might want to start with Repository pattern , an ideas on ruby implementation - here . 为了更好地理解这个概念,我建议观看Sandi Metz的演示 ,但是在您的特殊情况下,您可能希望从Repository模式开始,这是有关ruby实现的想法- 在这里

There is even some Ruby gem that provides some facilities for that: https://github.com/fredwu/datamappify 甚至还有一些Ruby gem提供了一些功能: https : //github.com/fredwu/datamappify

Being a pro software developer for just 6 years I can't go playing a super-duper smart guy. 作为专业软件开发人员仅6年,我无法扮演超级傻瓜型聪明人。 But it's been a year - year and half I have been incorporating different OOD patterns in my projects and I must say I wish that my OOP path would start with those. 但是已经一年半了,我一直在我的项目中加入不同的OOD模式,我必须说,我希望我的OOP道路将从这些开始。

While I haven't used repository pattern in any of my projects yet, I used others and I am eager to try this one out. 尽管我尚未在任何项目中使用存储库模式,但我使用了其他项目,因此我很想尝试一下。 Solid design takes time, doing things right takes time, but then it pays back as maintainable software. 坚实的设计需要时间,做正确的事需要时间,但随后它作为可维护的软件得到回报。 Question is though are You ready to take this path? 问题是您准备好走这条路了吗? :) :)

Did you try SQLite in-memory? 您是否尝试过SQLite内存? eg in config/database.yml in your Rails app you'd do: 例如,在Rails应用程序的config/database.yml中,您可以执行以下操作:

test:
  adapter: sqlite3
  encoding: utf8
  database: :memory:

There is also NullDB if you don't need read/write, but that probably isn't the case. 如果您不需要读/写,也可以使用NullDB ,但事实并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM