Nick Tomlin
Nick Tomlin

Reputation: 29251

isolating mongo operations in tests

I'm using the following code in a Ruby application (using Sequel) to roll the database back between test cases:

RSpec.configure do |config|
  config.around(:each) do |example|
    DB.transaction(:rollback=>:always, :auto_savepoint=>true){example.run}
  end
end

I'd love to have something similar in my Mongo tests, but I have only been able to find a manual process that requires a lot of specific information about the data I want to rollback. I'd like something more generic, similar to the ruby snippet above, that would just restore the DB to the state previous to each test block.

E.g.:

// psuedocode
var db = mongoose.connect('test-db');

afterEach(function () {
  db.rollback();
});

Upvotes: 0

Views: 486

Answers (2)

mienaikoe
mienaikoe

Reputation: 510

As of 2018, Mongo 4 and Mongoose 5.2 support transactions

https://mongoosejs.com/docs/transactions.html

https://www.mongodb.com/transactions

Upvotes: 1

Jeremy Evans
Jeremy Evans

Reputation: 12139

MongoDB does not support transactions (http://docs.mongodb.org/manual/faq/fundamentals/). If you want to use transactions, you'll have to use a different database. Most SQL databases support transactions.

Upvotes: 0

Related Questions