Reputation: 52651
I need orientation implementing the tests of a gem I'd like to build. It's a small gem that adds a new class method to ActiveRecord::Base
. This new method will execute some SQL on demmand.
Trouble is, the SQL to be executed depends on the database backend. It's different for SQLite, Postgres and MySQL. The ruby method signature is the same for all database backends - only the SQL changes.
So, how do I make the tests for this?
To clarify: I know how to ask ActiveRecord "what database backend are you on?". What I need is a way to tell RSpec/Minitest/TestUnit "change the database backend to Postgres, and try all tests again. And then MySQL".
Upvotes: 1
Views: 177
Reputation: 35308
Take a look at how DataMapper does this. They pass an environment variable ADAPTER
when running the specs, which alters the gems that are installed/loaded by bundler and changes the connection setup in the spec helper. You should be able to do something similar, basically running the full suite once for each possible database backend.
It looks like this when you run the DM core specs:
ADAPTER=mysql bundle install
ADAPTER=mysql bundle exec spec spec
ADAPTER=sqlite bundle install
ADAPTER=sqlite bundle exec rspec spec
I reckon if you want to get the best coverage, this is a good approach. If you want to selectively test different things for different parts of the tests, you'd just have to use conditionals, though (in general) I see conditionals in tests as a bad thing.
Upvotes: 2