Skip to content

Instantly share code, notes, and snippets.

@joshilewis
Created February 5, 2012 12:50
Show Gist options
  • Save joshilewis/1745366 to your computer and use it in GitHub Desktop.
Save joshilewis/1745366 to your computer and use it in GitHub Desktop.
Testing simple unit of work
public class MyClass
{
private readonly IDependency dependency;
private readonly Func<IUnitOfWork> StartNewUnitOfWork;
public MyClass(Func<IUnitOfWork> unitOfWorkProvider),
IDependency dependency,
{
this.dependency= dependency;
this.StartNewUnitOfWork = unitOfWorkProvider;
}
public void DoWork()
{
using (var uow = StartNewUnitOfWork())
{
uow.Begin(() => dependency.SomeMethod();
}
}
}
[Test]
public void TestDoWork()
{
var dependency = MockRepository.GenerateMock<IDependency>();
Action expectedAction = () => dependency.SomeMethod();
var uow = MockRepository.GenerateMock<IUnitOfWork>();
uow.Expect(u => u.Begin(expectedAction));
var uowProvider = MockRepository.GenerateMock<Func<IUnitOfWork>>();
uowProvider.Expect(u => u.Invoke());
var sut = new MyClass(uowProvider, dependency);
sut.DoWork();
uowProvider.VerifyAllExpectations();
uow.VerifyAllExpectations();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment