[英]Rhino Mocks Why Can't I Mock a Property?
I spent a few days writing a test, then had to add a property at the last minute to fix one of the issues I found in writing my test. 我花了几天时间编写测试,然后不得不在最后一刻添加一个属性,以解决我在编写测试时发现的问题之一。 Since adding that property I have been stuck just trying to get the mocking framework to function. 自添加该属性以来,我一直在尝试使模拟框架起作用。
Here is my code. 这是我的代码。
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();
}
I Know the AAA syntax is the new hotness but I have a large test that is complete but for this and I don't want to go back and rewrite. 我知道AAA语法是新的热点,但是我有一个完整的大型测试,但是为此,我不想返回并重写。
When code execution reaches the closing "}" of the "Playback" using I get this exception: 当代码执行到达使用“ Playback”的结尾“}”时,出现以下异常:
ExpectationViolationException ExpectationViolationException
TiersCalculationContainer.get_Round(); TiersCalculationContainer.get_Round(); Expected #1, Actual #0. 预期#1,实际#0。
When debugging the test the property "Round" is read correctly and retursn the value I mocked for it so I know it was called. 在调试测试时,正确读取了属性“ Round”并破坏了我为其模拟的值,因此我知道它被调用了。
I can't find any information online about this. 我在网上找不到有关此的任何信息。 There seems to be about 100 ways to mock a property in Rhino mocks. 在Rhino模拟中,大约有100种模拟属性的方法。 None of them are working and this is getting really frustrating. 他们都没有工作,这真令人沮丧。
I have also tried mocking all of these ways as well (and more) 我也尝试过模拟所有这些方式(还有更多)
_mockTiersHelper.Expect(b => b.Round).Return(round);
Expect.Call(_mockTiersHelper.Round).PropertyBehavior();
_mockTiersHelper.Round = round;
That is a lot of expectations for one test, I would recommend testing the behavior of each of these objects separately and then testing only that they are called properly at the integration point. 对一个测试有很多期望,我建议分别测试每个对象的行为,然后仅测试在集成点是否正确调用了它们。
Aside from that I think your problem is a logic issue not a syntax issue, if you are setting up the mock according to the documentation and getting an unexpected behavior its a bug in your code or your test. 除此之外,我认为您的问题是逻辑问题,而不是语法问题,如果您要根据文档设置模拟并获得意外行为,则这是代码或测试中的错误。
I think the answer to this may be that this is a bug. 我认为答案可能是错误。 I dumped Rhino and went back to Moq. 我抛弃了犀牛,回到了起订量。 10 minutes and I was up and running. 10分钟,我就开始跑步了。 Now my tests pass. 现在我的测试通过了。 Thank you Moq! 谢谢你,定单!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.