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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
How to mock this[Object value] which has a get accessor???
asked by nishaa (600 points)

1 Answer

0 votes
Hi
To mock an indexer use Mock.ExpectGetIndex method
example:

class TestedClass
{        
   public string this[int index]
   {
      //we are mocking the getter implementation is not important here
      get { throw new NotImplementedException(); }
   }
}

[TestFixture]
public class TestClass
{
   [Test]
   public void Test()
   {
      Mock mock = MockManager.Mock(typeof (TestedClass));
      mock.ExpectGetIndex("abc");

      TestedClass c = new TestedClass();
      Assert.AreEqual("abc", c[0]);
   }
}

Hope it helps
answered by ohad (35.4k points)
...