C# Rhino 使用 AAA 在属性 getter 上模拟 AssertWasCalled(多次)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/729267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rhino Mocks AssertWasCalled (multiple times) on property getter using AAA
提问by Confused
I have a mocked object that is passed as a constructor argument to another object.
我有一个模拟对象,它作为构造函数参数传递给另一个对象。
How can I test that a mocked object's property has been called? This is code I am using currently:
如何测试模拟对象的属性是否已被调用?这是我目前使用的代码:
INewContactAttributes newContact = MockRepository.GenerateMock<INewContactAttributes>();
newContact.Stub(x => x.Forenames).Return("One Two Three");
someobject.ConsumeContact(newContact);
newContact.AssertWasCalled(x => { var dummy = x.Forenames; });
This works except when within the "someobject" the getter on Forenames property is used multiple times. That's when I get "Rhino.Mocks.Exceptions.ExpectationViolationException: INewContactAttributes.get_Forenames(); Expected #1, Actual #2.."
这有效,除非在“someobject”中 Forenames 属性上的 getter 被多次使用。那是我得到“Rhino.Mocks.Exceptions.ExpectationViolationException: INewContactAttributes.get_Forenames(); 预期#1,实际#2..”的时候
Simply using
简单地使用
newContact.AssertWasCalled(x => { var dummy = x.Forenames; }, options => options.Repeat.Any());
does not work and gives the error below:
不起作用并给出以下错误:
"The expectation was removed from the waiting expectations list, did you call Repeat.Any() ? This is not supported in AssertWasCalled()."
“期望已从等待期望列表中删除,您是否调用了 Repeat.Any() ?AssertWasCalled() 不支持此功能。”
So how do I cater for the multiple calls?
那么我如何满足多个呼叫的需求呢?
采纳答案by Lennaert
newContact.AssertWasCalled(x => { var dummy = x.Forenames; }, options => options.Repeat.AtLeastOnce());
newContact.AssertWasCalled(x => { var dummy = x.Forenames; }, options => options.Repeat.AtLeastOnce());
Repeat.Any
does not work with AssertWasCalled
because 0 counts as any... so if it WASN'T called, the AsserWasCalled
would return TRUE even if it wasn't called.
Repeat.Any
不起作用,AssertWasCalled
因为 0 算作任何...所以如果它没有被调用,AsserWasCalled
即使没有被调用,它也会返回 TRUE。
回答by Garry Shutler
What is your motivation behind checking the number of times it is called? Is it a particularly expensive operation? If so, then I would suggest that you put it behind a method instead as, semantically speaking, properties should be inexpensive calls.
检查它被调用的次数背后的动机是什么?这是一项特别昂贵的手术吗?如果是这样,那么我建议你把它放在一个方法后面,因为从语义上讲,属性应该是廉价的调用。
Also, checking the number of times a property is called is not the thrust of unit testing (don't worry it's a common mistake to test too much, we've all been there). What you should really be testing is that given the state of your mock object that the method produces the expected output. The number of times a method is called to do that doesn't really matter (unless it's a service to send an email or something). It is an implementation detail which you normally wouldn't test as a simple refactor would break your tests as they would be too specific.
此外,检查属性被调用的次数并不是单元测试的重点(不要担心测试太多是一个常见的错误,我们都经历过)。您真正应该测试的是,给定模拟对象的状态,该方法会产生预期的输出。调用方法的次数并不重要(除非它是发送电子邮件或其他东西的服务)。这是一个您通常不会测试的实现细节,因为简单的重构会破坏您的测试,因为它们太具体了。
回答by Lennaert
newContact.Expect( c => c.ForeNames ).Return( ... ).Repeat.Any()
newContact.Expect( c => c.ForeNames ).Return( ... ).Repeat.Any()
回答by Llyle
Depending on your version of Rhino you are using, you can use:
根据您使用的 Rhino 版本,您可以使用:
// Call to mock object here
LastCall.IgnoreArguments().Repeat.Never();
回答by Usman
I agree with chris answer
我同意克里斯的回答
newContact.AssertWasCalled(x => { var dummy = x.Forenames; }, options => options.Repeat.AtLeastOnce());
Additionally If you know exactly how many times the property would be called you can do
此外,如果您确切知道该属性将被调用多少次,您可以这样做
newContact.AssertWasCalled(x => { var dummy = x.Forenames; }, options => options.Repeat.Times(n));
where n is an int.
其中 n 是一个整数。