Created
February 5, 2012 12:50
-
-
Save joshilewis/1745366 to your computer and use it in GitHub Desktop.
Testing simple unit of work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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