【发布时间】:2011-08-06 09:58:34
【问题描述】:
我想测试以下代码:
foreach (CallQueueData queueData in _configurationProvider.CallQueues.Values)
{
_chimeManager.AddConfig(new ChimeConfigData(
ChimeKey.Create(ChimeType.CallQueue, queueData.Name));
if (queueData.LowChime != null)
{
_chimeManager.AddConfig(new ChimeConfigData(
ChimeKey.Create(ChimeType.QueueLowChime, queueData.Name));
}
if (queueData.HighChime != null)
{
_chimeManager.AddConfig(new ChimeConfigData(
ChimeKey.Create(ChimeType.QueueHighChime, queueData.Name));
}
}
我的一个测试如下所示:
public void ShouldAddHighChimeConfigToChimeManager_IfServiceIsStarted_AndHighChimeIsConfigured()
{
// GIVEN
InitializeObjectUnderTestWithougStartingService();
var callQueueData = new CallQueueData
{
Name = "Emergency",
HighChime = new ChimeType(1, "High")
};
CallQueues.Add(callQueueData.Id, callQueueData);
// WHEN
_mgr.Startup();
// THEN
ChimeManager.AssertWasCalled(x => x.AddConfig(Arg<ChimeConfigData>.Matches(
y => y.Key == ChimeKey.Create(ChimeType.HighChime, callQueueData.Name))));
}
这里的问题是多次调用ChimeManager的AddConfig方法 而且我不想指定在它与我的方法匹配之前必须调用它的频率。
// i dont like this repeat twice because this ties the test code to much to the production code
ChimeManager.AssertWasCalled(x => x.AddConfig(Arg<ChimeConfigData>.Matches(
y => y.Key == ChimeKey.Create(ChimeType.HighChime, callQueueData.Name)),
y => y.Repeat.Twice));
我宁愿说这样的话:
ChimeManager.AssertWasCalled(x => x.AddConfig(Arg<ChimeConfigData>.Matches(
y => y.Key == ChimeKey.Create(ChimeType.HighChime, callQueueData.Name)),
y => y.Repeat.Any / y.Match.Any));
不幸的是,Repeat.Any 在这种情况下是无效的,any 没有 Match.Any。
我如何断言这个方法是用指定的参数调用的,但它被调用的频率并不重要。当对方法的调用之一与指定的参数匹配时,断言将不会失败。
【问题讨论】:
标签: unit-testing mocking rhino-mocks