如何断言某个方法仅被调用一次?
[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<ICanBeProcessedOnceADay>();
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()));
}
编辑后的解决方案:
[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);
}
[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<ICanBeProcessedOnceADay>();
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将对
stub.Expect()
和stub.VerifyAllExpectations()
的调用替换为stub.AssertWasCalled(x => x.Process(), o =>
。如果您对存根有多个期望,则可以将每个断言放入一个It
中的 o.Repeat.Once())It
中,并让它们彼此独立地失败(或成功)。存根的创建将进入
Establish
(本质上,任何依赖项和被测系统的创建是单元测试中“安排”阶段的一部分)。另请考虑不要使用
GenerateMock
而是使用GenerateStub
,因为当您调用Expect
指定的方法以外的其他方法时,模拟可能会导致脆弱的测试。像 FakeItEasy 这样的库通常提供更好、更容易发现的 API,更容易学习,并且会让你掉进“成功的坑”。I would replace the calls to
stub.Expect()
andstub.VerifyAllExpectations()
withstub.AssertWasCalled(x => x.Process(), o => o.Repeat.Once())
in theIt
. If you have more than one expectation against the stub you can then put each assertion in oneIt
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
butGenerateStub
as mocks will likely lead to brittle tests when you call other methods than the ones specified withExpect
. Libraries like FakeItEasy generally provide better and more discoverable APIs, are easier to learn and will make you fall into the "pit of success".如果你想确保一个方法仅被调用一次,你需要一个严格的模拟:
如果你只使用GenerateMock,它会执行一个“至少”检查。这也适用于 Repeats.Times(x) 。
If you want to ensure that a method is called only once, you need a strict mock:
If you just use GenerateMock, it will perform an "at least" check. This also goes for Repeats.Times(x) too.