Skip to content

Instantly share code, notes, and snippets.

@alecwhittington
Created September 29, 2011 15:19
Show Gist options
  • Save alecwhittington/1250958 to your computer and use it in GitHub Desktop.
Save alecwhittington/1250958 to your computer and use it in GitHub Desktop.
Testing a Task
[TestFixture]
public class MyTaskTests
{
private MyTask sut;
private IRepository<Help> helpRepository;
private IAnotherRepository anotherRepository;
[Test]
public void My_Sample_Test()
{
// Arrange
Another result;
Another toSave = new Another { MyValue = 1 };
this.anotherRepository.Stub(a => a.SaveOrUpdate(null)).IgnoreArguments().Return(new Another());
//// You could create a return item above the previous line - assign it properties, etc, then return that instead.
// Act
result = this.sut.SaveOrUpdate(toSave);
// Assert
this.anotherRepository.AssertWasCalled(a => a.SaveOrUpdate(Arg<Another>.Is.Anything));
//// Other assertions here.
}
[SetUp]
public void SetUp()
{
this.helpRepository = MockRepository.GenerateStub<IRepository<Help>>();
this.anotherRepository = MockRepository.GenerateStub<IAnotherRepository>();
this.sut = new MyTask(this.helpRepository, this.anotherRepository);
}
[TearDown]
public void TearDown()
{
this.sut = null;
this.helpRepository = null;
this.anotherRepository = null;
}
}
public class MyTask : IMyTask
{
private readonly IRepository<Help> helpRepository;
private readonly IAnotherRepository anotherRepository;
public MyTask(IRepository<Help> helpRepository, IAnotherRepository anotherRepository)
{
this.helpRepository = helpRepository;
this.anotherRepository = anotherRepository;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment