Put simply, I have a WCF service that manages apples. Apart from other functionality, it has two methods to add and remove apples from storage. I am writing an integration test to check if someone is getting advantage of the job and nicks apples. Raven DB in my WCF service has an audit role, it just records actions and apples. In the methods of WCF service there is some other processing: cleaning, validation, packaging etc.
My audit integration test can be expresses as
- Empty storage (RavenDB in-memory mode)
- Bob comes and puts 10 apple (open session, add, dispose session)
- Jake comes and takes 4 apples (open session, remove, dispose session)
- Check that 6 apples left
As these are two different people (two WCF calls) it make sense to use different instances of session. However, with Raven DB I get
Exception
Apple is not associated with the session, cannot delete unknown entity
instance
If I now run similar integration test where two different people just add apples to the storage, the total storage content corresponds to truth. This is confusing bit: adding works across session, removing doesn’t work. In this post Ayende says session micro-managing is not the way to go, but it seems natural to me to use different sessions in my integration testing. Hope analogy with apples doesn’t put you off.
Question: How do I use sessions in integration testing with RavenDB?
Sample code (from notepad)
public void Remove(Apple apple)
{
using (var session = Store.OpenSession())
{
session.Delete(apple);
session.SaveChanges();
}
}
public void Add(Apple apple)
{
using (var session = Store.OpenSession())
{
session.Store(apple);
session.SaveChanges();
}
}
...
var apples = new apples[10];
//init
MyRavenDB.Add(apples);
MyRavenDB.Remove(apples.Take(4)); //throws here
//verify
In RavenDB, “The session manages change tracking for all of the entities that it has either loaded or stored”.
I suspect the
Applereference you are passing toRemove()method, did not originate from RavenDB Document Store, hence the error.Try this: