TheTrueMzungu
TheTrueMzungu

Reputation: 45

Rspec testing against local DB

I am currently running a application that is moving data from an API into a MySQL database with Ruby 2.1.4 with MySQL2 gem.

I am trying to create RSPEC tests for this application but the problem is that the application is mainly MySQL queries and I do not want to mock out the query calls as I want to test my queries to make sure they are working as expected.

I have tried to do it by changing the client to sqlite3 gem when running rspec locally but the issue is that SQLite3 and MySQL2 don't handle queries the same. (Mysql2 uses Client.query where SQLite uses Client.execute).

I am attempting to emulate rails + rspec with how it uses sqlite locally but mysql remotely.

How could I go about doing this locally without having to spin up a external server? Do I need to use ActiveRecord or is there some way to create a CSV file that I can pass to the MySQL client I am creating at the beginning of the application?

I appreciate any help I can get or any suggestions you might have as to how I can adjust my approach to this issue. Code snipppits are below:

RSPEC Tests RSPEC test

Ruby Application Ruby Application

Upvotes: 1

Views: 851

Answers (1)

Anthony E
Anthony E

Reputation: 11235

Rather than try to fit a round peg in a square hole, why not just entirely use MySQL in your test environment? sqlite3 doesn't always accurately emulate MySQL so you're likely to get more reliable testing by just setting up and using MySQL on your local machine. You may find your tests may run faster as well.

Personally, I always test against the same type of database I'm running in production just to avoid issues like the one you describe.

Upvotes: 1

Related Questions