How to write tests for ActiveRecord extension gem

384 views Asked by At

I'm creating a Ruby gem that will extend the ActiveRecord functionality. I read the following Stack Overflow posts of how to extend ActiveRecord: post 1, post 2.

I'm using rspec for testing as describe in this great tutorial.

My question is, how do I test my active record extension methods using Rspec, because as I can think, we are not using a DB to save records.

How can I write tests for these kind of gems?

2

There are 2 answers

1
BroiSatse On BEST ANSWER

You probably want to have a look at acts_as_fu gem: https://github.com/nakajima/acts_as_fu.

Btw, you can use db in your rspec tests, but you don;t want to do this to test extensions.

1
Adam D Harrington On

if you didn't come up with a solution yet I was struggling with the same issues and and eventually came across this tutorial. It uses a really solid approach and works like a dream.

Basically, it involves referencing an active record file in the test helper which your test itself creates from a schema and seed file.

This approach was surprisingly simple to implement and helped me better understand how ActiveRecord extensions worked. So cheers Ryan!

Hope it helps.