views:

491

answers:

1

I have the following Unit Test method:

void TestOrderItemDelete()
{
    using (new SessionScope())
    {
        var order = Order.FindById(1234);
        var originalItemCount = order.OrderItems.Count;
        Assert.IsTrue(originalCount > 0);
        var itemToDelete = order.OrderItems[0];
        itemToDelete.DeleteAndFlush(); // itemToDelete.Delete();
        order.Refresh();
        Assert.AreEqual(originalCount - 1, order.OrderItems.Count);
    }
}

As you can see from the comment after the DeleteAndFlush command, I had to change it from a simple Delete to get the Unit test to pass. Why is this? The same is not true for my other unit test for adding an OrderItem. This works just fine:

void TestOrderItemAdd()
{
    using (new SessionScope())
    {
        var order = Order.FindById(1234);
        var originalItemCount = order.OrderItems.Count;
        var itemToAdd = new OrderItem();
        itemToAdd.Order = order;
        itemToAdd.Create(); // Notice, this is not CreateAndFlush
        order.Refresh();
        Assert.AreEqual(originalCount + 1, order.OrderItems.Count);
    }
}

All of this came up when I started using Lazy Instantiation of the Order.OrderItems relationship mapping, and had to add the using(new SessionScope) block around the test.

Any ideas?

A: 

This is difficult to troubleshoot without knowing the contents of your mappings, but one possibility is that you have the ID property of the OrderItem mapped using an identity field (or sequence, etc.) in the DB. If this is the case, NHibernate must make a trip to the database in order to generate the ID field, so the OrderItem is inserted immediately. This is not true of a delete, so the SQL delete statement isn't executed until session flush.

Sean Carpenter