Search code examples
c#unit-testingrhino-mocks

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.

When code execution reaches the closing "}" of the "Playback" using I get this exception:

ExpectationViolationException

TiersCalculationContainer.get_Round(); Expected #1, Actual #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.

I can't find any information online about this. There seems to be about 100 ways to mock a property in Rhino mocks. 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;

Solution

  • 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. Now my tests pass. Thank you Moq!