使用AAA的Rhino Mocks AssertWasCalled(多次)在属性getter上

Con*_*sed 51 c# getter unit-testing rhino-mocks properties

我有一个模拟对象作为构造函数参数传递给另一个对象.

如何测试已调用模拟对象的属性?这是我目前使用的代码:

INewContactAttributes newContact = MockRepository.GenerateMock<INewContactAttributes>();
newContact.Stub(x => x.Forenames).Return("One Two Three");
someobject.ConsumeContact(newContact);
newContact.AssertWasCalled(x => { var dummy = x.Forenames; });
Run Code Online (Sandbox Code Playgroud)

这有效,除非在"someobject"中,Forenames属性的getter被多次使用.那时我得到"Rhino.Mocks.Exceptions.ExpectationViolationException:INewContactAttributes.get_Forenames();期望#1,实际#2 .."

简单地使用

newContact.AssertWasCalled(x => { var dummy = x.Forenames; }, options => options.Repeat.Any());
Run Code Online (Sandbox Code Playgroud)

不起作用,并给出以下错误:

"期望已从等待期望列表中删除,您是否调用了Repeat.Any()?AssertWasCalled()不支持这种情况."

那么我该如何迎合多次通话呢?

小智 75

newContact.AssertWasCalled(x => { var dummy = x.Forenames; }, options => options.Repeat.AtLeastOnce());

Repeat.Any不起作用AssertWasCalled因为0计为任何...所以如果它没有被调用,AsserWasCalled即使它没有被调用也会返回TRUE.


Usm*_*man 26

我同意克里斯的回答

newContact.AssertWasCalled(x => { var dummy = x.Forenames; }, options => options.Repeat.AtLeastOnce());
Run Code Online (Sandbox Code Playgroud)

此外,如果您确切知道该物业的召唤次数,您可以这样做

newContact.AssertWasCalled(x => { var dummy = x.Forenames; }, options => options.Repeat.Times(n));
Run Code Online (Sandbox Code Playgroud)

其中n是int.