private void FakeManagerWithSubtransaction(TransactionHierarchyManager transactionHierarchyManager)
        {
            TransactionHierarchyManagerTestHelper.SetIsWriteable(transactionHierarchyManager, false);
            var fakeSubTransaction = ClientTransactionObjectMother.CreateWithParent(transactionHierarchyManager.ThisTransaction);

            TransactionHierarchyManagerTestHelper.SetSubtransaction(transactionHierarchyManager, fakeSubTransaction);
        }
        public void CreateSubTransaction()
        {
            var  counter = new OrderedExpectationCounter();
            bool subTransactionCreatingCalled = false;

            _thisEventSinkWithStrictMock.Expect(mock => mock.RaiseSubTransactionCreatingEvent())
            .WhenCalledOrdered(counter, mi =>
            {
                Assert.That(_manager.IsWriteable, Is.True);
                subTransactionCreatingCalled = true;
            });

            ClientTransaction fakeSubTransaction = ClientTransactionObjectMother.CreateWithParent(_thisTransaction);
            Func <ClientTransaction, ClientTransaction> factory = tx =>
            {
                Assert.That(tx, Is.SameAs(_thisTransaction));
                Assert.That(subTransactionCreatingCalled, Is.True);
                Assert.That(_manager.IsWriteable, Is.False, "IsWriteable needs to be set before the factory is called.");
                ClientTransactionTestHelper.SetIsWriteable(_thisTransaction, false); // required by assertion in ReadOnlyClientTransactionListener
                return(fakeSubTransaction);
            };

            _hierarchyStrictMock.Expect(mock => mock.AppendLeafTransaction(fakeSubTransaction)).Ordered(counter);
            _thisEventSinkWithStrictMock.Expect(mock => mock.RaiseSubTransactionCreatedEvent(fakeSubTransaction)).Ordered(counter);

            var result = _manager.CreateSubTransaction(factory);

            Assert.That(result, Is.Not.Null.And.SameAs(fakeSubTransaction));
            Assert.That(_manager.SubTransaction, Is.SameAs(fakeSubTransaction));
            Assert.That(_manager.IsWriteable, Is.False);

            _hierarchyStrictMock.VerifyAllExpectations();
        }
        public void CreateSubTransaction_InvalidFactory()
        {
            _thisEventSinkWithStrictMock.Expect(mock => mock.RaiseSubTransactionCreatingEvent());

            var fakeSubTransaction = ClientTransactionObjectMother.CreateWithParent(null);
            Func <ClientTransaction, ClientTransaction> factory = tx => fakeSubTransaction;

            Assert.That(
                () => _manager.CreateSubTransaction(factory),
                Throws.InvalidOperationException.With.Message.EqualTo("The given factory did not create a sub-transaction for this transaction."));

            _thisEventSinkWithStrictMock.AssertWasNotCalled(mock => mock.RaiseSubTransactionCreatedEvent(Arg <ClientTransaction> .Is.Anything));
            _thisEventSinkWithStrictMock.VerifyAllExpectations();

            Assert.That(_manager.SubTransaction, Is.Null);
            Assert.That(_manager.IsWriteable, Is.True);
        }