Skip to content

Instantly share code, notes, and snippets.

@agross
Created September 26, 2009 18:52
Show Gist options
  • Save agross/194353 to your computer and use it in GitHub Desktop.
Save agross/194353 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Crimson.Infrastructure.Persistence;
using Machine.Specifications;
using Machine.Specifications.Utility;
using NHibernate;
using Rhino.Mocks;
namespace Crimson.Infrastructure.Tests.Persistence
{
public class When_a_unit_of_work_is_started : With_unit_of_work
{
Establish context = () => UoW.Start();
It should_start_a_new_transaction = () => Session.AssertWasCalled(x => x.BeginTransaction());
}
[Subject(typeof(NHibernateUnitOfWork))]
public class When_a_unit_of_work_is_committed : When_a_unit_of_work_is_started
{
Because of = () => UoW.Commit();
It should_commit_the_transaction = () => Transaction.AssertWasCalled(x => x.Commit());
It should_not_dispose_the_transaction = () => Transaction.AssertWasNotCalled(x => x.Dispose());
}
[Subject(typeof(NHibernateUnitOfWork))]
public class When_a_unit_of_work_is_rolled_back : When_a_unit_of_work_is_started
{
Because of = () => UoW.Rollback();
It should_rollback_the_transaction = () => Transaction.AssertWasCalled(x => x.Rollback());
It should_not_dispose_the_transaction = () => Transaction.AssertWasNotCalled(x => x.Dispose());
}
[Subject(typeof(NHibernateUnitOfWork))]
public class When_a_unit_of_work_is_disposed : When_a_unit_of_work_is_started
{
Because of = () => UoW.Dispose();
It should_dispose_the_transaction = () => Transaction.AssertWasCalled(x => x.Dispose());
It should_dispose_the_session = () => Session.AssertWasCalled(x => x.Dispose());
}
[Subject(typeof(NHibernateUnitOfWork))]
public class When_a_started_unit_of_work_is_started_again : When_a_unit_of_work_is_started
{
Because of = () => UoW.Start();
It should_dispose_the_running_transaction = () => Transaction.AssertWasCalled(x => x.Dispose());
It should_start_a_new_transaction = () => Session.AssertWasCalled(x => x.BeginTransaction(), o => o.Repeat.Twice());
}
[Subject(typeof(NHibernateUnitOfWork))]
public class When_a_disposed_unit_of_work_is_disposed_again : When_a_unit_of_work_is_started
{
Establish context = () => UoW.Dispose();
Because of = () => UoW.Dispose();
It should_not_dispose_the_session_again = () => Session.AssertWasCalled(x => x.Dispose());
}
[Subject(typeof(NHibernateUnitOfWork))]
public class When_a_disposed_unit_of_work_is_accessed : When_a_unit_of_work_is_started
{
static List<Exception> Exceptions;
static IEnumerable<MethodInfo> Methods;
Establish context = () =>
{
UoW.Dispose();
Methods = UoW
.GetType()
.GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance)
.Where(m => m.Name != "Dispose");
Exceptions = new List<Exception>();
};
Because of = () =>
Methods.Each(m =>
{
var args = m.GetParameters().Select(x => new object());
var exception = Catch.Exception(() => m.Invoke(UoW, args.ToArray()));
Exceptions.Add(exception);
});
It should_fail_for_each_method = () => Exceptions.Count().ShouldEqual(Methods.Count());
It should_fail = () => Exceptions.All(x => x.InnerException as ObjectDisposedException != null).ShouldBeTrue();
}
[Subject(typeof(NHibernateUnitOfWork))]
public class When_any_operation_except_starting_and_disposing_is_executed_on_an_unstarted_unit_of_work
: With_unit_of_work
{
static List<Exception> Exceptions;
static IEnumerable<MethodInfo> Methods;
Establish context = () =>
{
UoW.Dispose();
Methods = UoW
.GetType()
.GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance)
.Where(m => m.Name != "Start" && m.Name != "Dispose");
Exceptions = new List<Exception>();
};
Because of = () =>
Methods.Each(m =>
{
var args = m.GetParameters().Select(x => new object());
var exception = Catch.Exception(() => m.Invoke(UoW, args.ToArray()));
Exceptions.Add(exception);
});
It should_fail_for_each_method = () => Exceptions.Count().ShouldEqual(Methods.Count());
It should_fail = () => Exceptions.All(x => x.InnerException as InvalidOperationException != null).ShouldBeTrue();
}
public class With_unit_of_work
{
protected static ISession Session;
static INHibernateSessionFactory SessionFactory;
protected static ITransaction Transaction;
protected static NHibernateUnitOfWork UoW;
Cleanup after = () => UoW.Dispose();
Establish context = () =>
{
SessionFactory = MockRepository.GenerateStub<INHibernateSessionFactory>();
Session = MockRepository.GenerateStub<ISession>();
Transaction = MockRepository.GenerateStub<ITransaction>();
SessionFactory.Stub(s => s.CreateSession()).Return(Session);
Session.Stub(s => s.BeginTransaction()).Return(Transaction);
UoW = new NHibernateUnitOfWork(SessionFactory);
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment