chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
This code fails on the second call - I don't know why:
      [Test]
      [VerifyMocks]
      public void Test3()
      {
         MockObject MockedExchangeRateModel = MockManager.MockObject<ExchangeRate>(Constructor.Mocked);
         ExchangeRate ExchangeRateModel = (ExchangeRate)MockedExchangeRateModel.Object;

         using (RecordExpectations recorder = new RecordExpectations())
         {
            double d = ExchangeRateModel.NominalValue[0];
            recorder.Return(1.0).RepeatAlways();
         }
         double d1 = ExchangeRateModel.NominalValue[0];
         Assert.AreEqual(1.0, d1);
         double d2 = ExchangeRateModel.NominalValue[0]; // null reference here
         Assert.AreEqual(d1, d2);
      }

System.NullReferenceException: Object reference not set to an instance of an object.

Note:
ExchangeRateModel.NominalValue[0] is a default indexed property.
I think the null reference happens because the NominalValue[0] call is not mocked and it is going directly to ExchangeRateModel (which has not been initialised).
asked by hiegunn (1.9k points)

2 Answers

0 votes
The following code works but:
1) Why do I need two expectations?
2) Why doesn't Return work even if I put it twice?

      [Test]
      [VerifyMocks]
      public void Test3()
      {
         MockObject MockedExchangeRateModel = MockManager.MockObject<ExchangeRate>();
         ExchangeRate ExchangeRateModel = (ExchangeRate)MockedExchangeRateModel.Object;

         using (RecordExpectations recorder = new RecordExpectations())
         {
            // doesn't work
            //double d = ExchangeRateModel.NominalValue[0];
            //recorder.Return(1.0).RepeatAlways(); 
            //recorder.Return(1.0).RepeatAlways();
            //does work
            recorder.ExpectAndReturn(ExchangeRateModel.NominalValue[0], 1.0).RepeatAlways();
            recorder.ExpectAndReturn(ExchangeRateModel.NominalValue[0], 1.0).RepeatAlways();
         }

         double d1 = ExchangeRateModel.NominalValue[0];
         Assert.AreEqual(1.0, d1);
         double d2 = ExchangeRateModel.NominalValue[0];
         Assert.AreEqual(d1, d2);
      }
answered by hiegunn (1.9k points)
0 votes
Hi,

Everything comes to the fact that RepeatAlways is only applied to the last call of the chain:
ExchangeRateModel.NominalValue[0];


In this case the only the call to "[0]" will be repeated and the calls to
ExchangeRateModel.NominalValue will be mocked once - per recording statement.

In your first post ont the second call:
double d2 = ExchangeRateModel.NominalValue[0]; // null reference here

The real NominalValue property is activated and since the underlying array is not initialized you get an exception.

In your second post, in the part which does work you actauly put two expecatation for the calls to NominalValue.

:!: As I mentioned in previous posts, this behavior of RepeatAlways is something we consider as an issue to be changed/fixed.
answered by lior (13.2k points)
...