Domanda

i'm looking for some gem which allows me to stub AR with in memory array.

So, all finds will work, but real db wouldn't be touched.

Please advice,

Thanks!

È stato utile?

Soluzione

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.

But I guess it's speed You are after. In this case solid OO design is Your friend. 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). 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.

There is even some Ruby gem that provides some facilities for that: https://github.com/fredwu/datamappify

Being a pro software developer for just 6 years I can't go playing a super-duper smart guy. 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.

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? :)

Altri suggerimenti

Did you try SQLite in-memory? e.g. in config/database.yml in your Rails app you'd do:

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top