I've found one interesting article here
http://www.agileprogrammer.com/oneagilecoder/archive/2007/09/02/23265.aspx
The author introduces "roles" via mocking the objects, starting with
acceptance tests.
What was strange for me, he used more mock objects in one test.
I took his example and rewrite the test (first one) using AAA style.
Then I created other 3 tests - using just one mock object per test
(more stubs are allowed) as Roy Osherove recommends in his The Art Of
Unit Testing book.
@everyone:
Do you ever use more mocks in one test?
Isn't it better to see "bigger" picture in one tests (how the objects
cooperate)?
Does not these 3 tests introduce some kind of duplicity?
How would your test(s) look like for this method?
@Ayende: if you are not too busy, please reply what do you prefer
Thanks for answers
and here is the code:
namespace PayrollLibrary.UnitTests
{
[TestFixture]
public class PayrollSystemFixture
{
[Test]
public void CreateControlFlowThroughSystem()
{
IInputReader reader = MockRepository.GenerateMock<IInputReader>();
IProcessor processor = MockRepository.GenerateMock<IProcessor>();
IOutputWriter writer =
MockRepository.GenerateMock<IOutputWriter>();
PayrollSystem payrollSystem = new PayrollSystem(reader, processor,
writer);
List<BatchInput> inputs = new List<BatchInput>();
List<ProcessOutput> outputs = new List<ProcessOutput>();
reader.Stub(x => x.ReadAllInputs()).Return(inputs);
processor.Stub(x => x.Process(inputs)).Return(outputs);
payrollSystem.Run();
reader.AssertWasCalled(x => x.ReadAllInputs(), x =>
x.Repeat.Once());
processor.AssertWasCalled(x => x.Process(inputs), x =>
x.Repeat.Once());
writer.AssertWasCalled(x => x.WriteAllOutputs(outputs), x =>
x.Repeat.Once());
}
[Test]
public void Run_WhenCalled_ReadsAllInputsFromReader()
{
IInputReader mockReader =
MockRepository.GenerateMock<IInputReader>();
IProcessor processor = MockRepository.GenerateStub<IProcessor>();
IOutputWriter writer =
MockRepository.GenerateStub<IOutputWriter>();
PayrollSystem payrollSystem = new PayrollSystem(mockReader,
processor, writer);
payrollSystem.Run();
mockReader.AssertWasCalled(x => x.ReadAllInputs(), x =>
x.Repeat.Once());
}
[Test]
public void Run_WhenCalled_ProcessesInputs()
{
IInputReader reader = MockRepository.GenerateStub<IInputReader>();
IProcessor mockProcessor =
MockRepository.GenerateMock<IProcessor>();
IOutputWriter writer =
MockRepository.GenerateStub<IOutputWriter>();
PayrollSystem payrollSystem = new PayrollSystem(reader,
mockProcessor, writer);
List<BatchInput> inputs = new List<BatchInput>();
reader.Stub(x => x.ReadAllInputs()).Return(inputs);
payrollSystem.Run();
mockProcessor.AssertWasCalled(x => x.Process(inputs), x =>
x.Repeat.Once());
}
[Test]
public void Run_WhenCalled_WritesAllOutputs()
{
IInputReader reader = MockRepository.GenerateStub<IInputReader>();
IProcessor processor = MockRepository.GenerateStub<IProcessor>();
IOutputWriter mockWriter =
MockRepository.GenerateMock<IOutputWriter>();
PayrollSystem payrollSystem = new PayrollSystem(reader, processor,
mockWriter);
List<BatchInput> inputs = new List<BatchInput>();
List<ProcessOutput> outputs = new List<ProcessOutput>();
reader.Stub(x => x.ReadAllInputs()).Return(inputs);
processor.Stub(x => x.Process(inputs)).Return(outputs);
payrollSystem.Run();
mockWriter.AssertWasCalled(x => x.WriteAllOutputs(outputs), x =>
x.Repeat.Once());
}
}
}
--
You received this message because you are subscribed to the Google Groups
"Rhino.Mocks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rhinomocks?hl=en.