【发布时间】:2012-09-14 23:43:04
【问题描述】:
我花了几天时间编写测试,然后不得不在最后一刻添加一个属性来解决我在编写测试时发现的一个问题。自从添加该属性后,我一直在试图让模拟框架发挥作用。
这是我的代码。
using (_mockRepository.Record())
{
_mockBattleDao.Expect(b => b.GetUnprocessedActions(gameId, round)).Return(roundResolvingItems);
_mockDao.Expect(b => b.GetMidGameCharacterStats(gameId, round)).Return(midGameCharacterStats);
_mockBattleDao.Expect(b => b.GetAmbientCharacterBuffs(_mockTiersHelper, gameId, round)).Return(new List<Buff>());
_mockBattleDao.Expect(b => b.GetActiveTriggerBuffs(_mockTiersHelper, gameId, round)).Return(triggerBuffs);
_mockBattleDao.Expect(b => b.GetActiveAmbientBuffs(_mockTiersHelper, gameId, round)).Return(new List<Buff>());
_mockDao.Expect(b => b.GetGame(gameId)).Return(new Common.Entities.Game { CompletionType = "single party down" });
_mockDao.Expect(b => b.GetAbilityById(1337)).Return(ability).Repeat.Times(3);
_mockDao.Expect(b => b.GetAbilityById(1727)).Return(attackAbility).Repeat.Times(4);
_mockTiersHelper.Expect(b => b.AddStatistic(Arg<StatAndCount>.Is.Anything)).Repeat.Times(3);
SetupResult.For(_mockTiersHelper.Round).Return(round);
}
TiersCalculationContainer container;
using (_mockRepository.Playback())
{
container = engine.ProcessTiers();
}
我知道 AAA 语法是新的热门,但我有一个已完成的大型测试,但为此我不想回去重写。
当代码执行到达“播放”的结束“}”时,我得到了这个异常:
ExpectationViolationException
TiersCalculationContainer.get_Round();预期 #1,实际 #0。
在调试测试时,“Round”属性被正确读取并返回我为它模拟的值,所以我知道它被调用了。
我在网上找不到任何关于此的信息。在 Rhino 模拟中似乎有大约 100 种模拟属性的方法。他们都没有工作,这真的令人沮丧。
我也尝试过模拟所有这些方式(以及更多方式)
_mockTiersHelper.Expect(b => b.Round).Return(round);
Expect.Call(_mockTiersHelper.Round).PropertyBehavior();
_mockTiersHelper.Round = round;
【问题讨论】:
-
我认为这个问题的答案可能是这是一个错误。我甩掉了犀牛,回到了起订量。 10 分钟,我就开始跑步了。现在我的测试通过了。谢谢你起订量!
标签: c# unit-testing rhino-mocks