Unit Testing RavenDB
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 reaso开发者_如何学Gon 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.
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
Try to use RunInUnreliableYetFastModeThatIsNotSuitableForProduction = true
.
var _store = new EmbeddableDocumentStore()
{
Configuration =
{
RunInUnreliableYetFastModeThatIsNotSuitableForProduction = true,
RunInMemory = true,
}
};
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.
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
精彩评论