30

In my unit tests I am setting up each test to have a totally empty IDocumentSession. I do it like this:

[SetUp]
public void SetUp()
{
  _store = new EmbeddableDocumentStore
  {
     RunInMemory = true
  };

  _store.Initialize();

  Session = _store.OpenSession();
}

But I think this might be the reason my tests are a little slow. I was wondering if there is a simple command to delete all documents from the database.

What I want is to know is: if I can do this, and if it would improve performance.

ccellar
  • 10,326
  • 2
  • 38
  • 56
nick
  • 1,477
  • 2
  • 20
  • 29

4 Answers4

25

This is the recommended approach for unit testing with ravendb The not recommended for production basically runs in the in memory mode If you find this to be slow, try profiling and figuring out what exactly is slowing things down

Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41
23

Try to use RunInUnreliableYetFastModeThatIsNotSuitableForProduction = true.

        var _store = new EmbeddableDocumentStore()
        {
            Configuration =
                {
                    RunInUnreliableYetFastModeThatIsNotSuitableForProduction = true,
                    RunInMemory = true,
                }
        };
Mo Valipour
  • 13,286
  • 12
  • 61
  • 87
Daniel Lang
  • 6,819
  • 4
  • 28
  • 54
  • 2
    I thought you were joking at first. It doesn't seem too make too much difference though, although maybe a little bit – nick Sep 23 '11 at 19:31
  • Yes, I was also overwhelmed when I saw the length of this property first. However, if you look at RavenDBs own source-code you will find that all their unit-tests create a new store like the way above. It think it's by far the most secure and fail-safe way to create a new one instead of deleting all the contents. I really wouldn't recommend that. – Daniel Lang Sep 23 '11 at 19:51
6

The expensive call there is the _store.Initialize() -- you are forcing RavenDb to stand up a new database every test. In most cases a single database per test suite run will work.

Another option would be to use the nature or RavenDb's IDs to namespace your tests. This is pretty handy if the real issue is duplicate key errors and otherwise engineering things so you don't have a nasty cleanup.

Wyatt Barnett
  • 15,573
  • 3
  • 34
  • 53
  • In terms of your first comment, that's pretty much what I thought. In terms of your recommendation, it seems a lot of work for what I need, but I'll have a look. Thanks Wyatt – nick Sep 23 '11 at 18:59
3

I know this is an old question but as of RavenDB 2.0 (not yet stable) there is a Raven Test Helper available as a Nuget package which is really useful when it comes to unit test RavenDB.

http://ravendb.net/docs/samples/raven-tests/createraventests?version=2.0

http://nuget.org/packages/RavenDB.Tests.Helpers/2.0.2198-Unstable

Nordis
  • 733
  • 2
  • 9
  • 31