[Subject(typeof(OnceADayProcessor))]
public class When_processing_process_twice
{
private static ICanBeProcessedOnceADay ProcessedOnceADay;
private Establish context = () => { OnceADayProcessor.Now = () => new DateTime(2011, 1, 1, 0, 0, 0, 0); };
private Because of = () =>
{
ProcessedOnceADay = MockRepository.GenerateMock<ICanBeProcessedOn开发者_运维知识库ceADay>();
ProcessedOnceADay.Process();
ProcessedOnceADay.Process();
};
private It should_execute = () => ProcessedOnceADay.AssertWasCalled(x => x.Expect(p => p.Process()));
private It should_execute_only_once = () => ProcessedOnceADay.AssertWasNotCalled(x => x.Expect(p => p.Process()));
}
edited solution:
[Subject(typeof(OnceADayProcessor))]
public class When_processing_a_process_twice_at_the_same_day
{
static ICanBeProcessedOnceADay canBeProcessedOnceADay;
Establish context = () =>
{
canBeProcessedOnceADay = A.Fake<ICanBeProcessedOnceADay>();
};
Because of = () =>
{
OnceADayProcessor.Process(canBeProcessedOnceADay);
OnceADayProcessor.Process(canBeProcessedOnceADay);
};
It should_execute_only_once = () =>
A.CallTo(() => canBeProcessedOnceADay.Process()).MustHaveHappened(Repeated.Exactly.Once);
}
var mock = MockRepository.GenerateMock<ICanBeProcessedOnceADay>();
mock.Expect(a => a.Process()).Repeat.Times(1);
...
mock.VerifyAllExpectations();
I would replace the calls to stub.Expect()
and stub.VerifyAllExpectations()
with stub.AssertWasCalled(x => x.Process(), o => o.Repeat.Once())
in the It
. If you have more than one expectation against the stub you can then put each assertion in one It
and have them fail (or succeed) independently of each other.
The creation of the stub would go into Establish
(essentially, creation of any dependencies and the System Under Test is part of the "arrange" phase in unit testing).
Also consider not to use GenerateMock
but GenerateStub
as mocks will likely lead to brittle tests when you call other methods than the ones specified with Expect
. Libraries like FakeItEasy generally provide better and more discoverable APIs, are easier to learn and will make you fall into the "pit of success".
If you want to ensure that a method is called only once, you need a strict mock:
var mock = MockRepository.GenerateStrictMock<IShouldOnlyBeCalledOnce>();
mock.Expect(a => a.Process()).Repeat.Once();
sut.Process(mock)
mock.VerifyAllExpectations();
If you just use GenerateMock, it will perform an "at least" check. This also goes for Repeats.Times(x) too.
精彩评论