Live geek or die tryin'

Sinatra, RSpec and DataMapper: Configuring and Using a Database for Tests

Hi there,

When testing your Sinatra application, you may need to check if some data was stored, removed, or changed in the database.

Your application would look like this:

Okay. First of all, let’s tell RSpec to use a blog_test.db database:

Explanations:

  • DataMapper::setup(): Tells DataMapper which database to use ; it will create it if it doesn’t exist.
  • DataMapper.finalize: Finalize the models. More information in DataMapper’s documentation.
  • auto_migrate! drops and recreate the tables, which is a good thing since we want to have a clean database before each test.

If it doesn’t already exist, a database will be created in the application’s root path the next time RSpec will be called (from the cli for example).

All what is left now is to write the test:

I hope this quick how-to was helpful.

Happy testing! :)

Comments