Using NUnit and Moq, I am trying to wrap some tests around legacy code before I do too much more refactoring. I need to test that a method keeps looping until told to stop. Here is my idea how to do it:
[TestCase]
public void KeepsCallingDoSomethingUntilShouldIKeepGoingIsFalse()
{
var dal = new Mock<IDataAccessLayer>();
var sut = new MyService(dal.Object);
int numberOfTimesToReturnTrue = 5;
dal.Setup(x => x.ShouldIKeepGoing())
.Callback(() => numberOfTimesToReturnTrue--)
.Returns(() => numberOfTimesToReturnTrue >= 0);
sut.Blah();
dal.Verify(x => x.DoSomething(), Times.Exactly(5));
}
Is this test hard to understand? Is this a valid/clean way to do this? Is there a better way? Something about writing a test this way bugs me.