title: How to set array elements with Moq?
body: <p>I am using Moq to mock a view and model to test a presenter.</p>
  
  <p>I would like to use mocking to confirm that array members of my model and view are indeed set in the ResetStatuses and UpdateTxtStatuses methods.</p>
  
  <p>However, running the test results in an error at my call to ResetStatuses, with the error message:</p>
  
  <pre><code>Result StackTrace:  
  at DSimGui.SocketStatusIGSSPresenter.ResetStatuses() in C:\dev\dir\DG\DSimGui\SocketStatusIGSSPresenter.cs:line 89
     at DSimGuiTest.SocketStatusIGSSTest.VerifyResetStatuses() in C:\dev\dir\DG\DSimGuiTest\SocketStatusIGSSTest.cs:line 82
  Result Message: 
  Test method DSimGuiTest.SocketStatusIGSSTest.VerifyResetStatuses threw exception: 
  System.IndexOutOfRangeException: Index was outside the bounds of the array.
  </code></pre>
  
  <p>I thought that this required me to use SetupSet to prepare the members for setting, but using SetupSet on txtClientStatuses resulted in another error:</p>
  
  <pre><code>Result StackTrace:  
  at DSimGuiTest.SocketStatusIGSSTest.&lt;&gt;c__DisplayClass1_0.&lt;VerifyResetStatuses&gt;b__3(SocketStatusIGSSIModel a) in C:\dev\dir\DG\DSimGuiTest\SocketStatusIGSSTest.cs:line 88
     at Moq.Mock.SetupSetImpl[T,TCall](Mock`1 mock, Action`1 setterExpression, Func`5 callFactory) in C:\projects\moq4\Source\Mock.cs:line 666
     at Moq.Mock.SetupSetPexProtected[T](Mock`1 mock, Action`1 setterExpression, Condition condition) in C:\projects\moq4\Source\Mock.cs:line 626
     at Moq.PexProtector.Invoke[T1,T2,T3,TResult](Func`4 function, T1 arg1, T2 arg2, T3 arg3) in C:\projects\moq4\Source\PexProtector.cs:line 75
     at DSimGuiTest.SocketStatusIGSSTest.VerifyResetStatuses() in C:\dev\dir\DG\DSimGuiTest\SocketStatusIGSSTest.cs:line 88
  Result Message: 
  Test method DSimGuiTest.SocketStatusIGSSTest.VerifyResetStatuses threw exception: 
  System.IndexOutOfRangeException: Index was outside the bounds of the array.
  </code></pre>
  
  <p>How do I correctly call SetupSet for array indexes? Assuming that this will allow the presenter to set array elements without error.</p>
  
  <p>My code is listed below:</p>
  
  <p><strong>Presenter</strong></p>
  
  <pre><code>public class SocketStatusIGSSPresenter
  {
      private readonly SocketStatusIGSSIView view;
      private readonly SocketStatusIGSSIModel model;
      private readonly EventHub eventHub;
  
      public SocketStatusIGSSPresenter(
          SocketStatusIGSSIView view,
          SocketStatusIGSSIModel model)
      {
          this.view = view;
          view.Presenter = this;
  
          this.model = model;
          Initialize();
      }
  
      private void Initialize()
      {
          model.txtClientStatuses = new string[4];
          model.txtServerStatuses = new string[4];
          model.txtIGSSCmd = "";
      }
  
      public void ResetStatuses()
      {
          for (int i = 1; i &lt;= 3; i++)
          {
              model.txtClientStatuses[i] = "0";
              model.txtServerStatuses[i] = "0";
          }
          UpdateTxtStatuses();
      }
  
      public void UpdateTxtStatuses()
      {
          for (int i = 1; i &lt;= 3; i++)
          {
              view.txtClientStatuses[i].Text = model.txtClientStatuses[i];
              view.txtServerStatuses[i].Text = model.txtServerStatuses[i];
          }
      }
  }
  </code></pre>
  
  <p><strong>Test Method</strong></p>
  
  <pre><code>[TestMethod]
  public void VerifyResetStatuses()
  {
      var model = new Mock&lt;SocketStatusIGSSIModel&gt;();
      model.SetupSet(a =&gt; a.txtClientStatuses = It.IsAny&lt;string[]&gt;());
      model.SetupSet(a =&gt; a.txtServerStatuses = It.IsAny&lt;string[]&gt;());
      model.SetupSet(a =&gt; a.txtIGSSCmd = It.IsAny&lt;string&gt;());
  
      var view = new Mock&lt;SocketStatusIGSSIView&gt;();
      var eventHub = new EventHub();
      var presenter = new SocketStatusIGSSPresenter(view.Object, model.Object, eventHub);
  
      // Results in error!
      //for (int i = 0; i &lt; 4; i++)
      //{
      //  model.SetupSet(a =&gt; a.txtClientStatuses[i] = It.IsAny&lt;string&gt;());
      //}
  
      // Results in error!
      //presenter.ResetStatuses();
  
      // This will be used to verify that txtClientStasuses array elements are set
      //model.VerifySet(a =&gt; a.txtClientStatuses[It.IsAny&lt;int&gt;()] = It.IsAny&lt;string&gt;(), Times.Exactly(3));
  }
  </code></pre>
  
  <p><strong>Model interface</strong></p>
  
  <pre><code>public interface SocketStatusIGSSIModel
  {
      string[] txtClientStatuses { get; set; }
  
      string[] txtServerStatuses { get; set; }
  
      string txtIGSSCmd { get; set; }
  
      string TxtDataValue { get; set; }
  
      short TxtSrcSeqCnt { get; set; }
  }
  </code></pre>
  
  <p><strong>View interface</strong></p>
  
  <pre><code>public interface SocketStatusIGSSIView
  {
      SocketStatusIGSSPresenter Presenter { set; }
  
      TextBox[] txtClientStatuses { get; set; }
  
      TextBox[] txtServerStatuses { get; set; }
  
      string TxtIGSSCmd { get; set; }
  
      string TxtDataValue { get; set; }
  
      string TxtSrcSeqCntValue { get; set; }
  
      void Show();
  
      void Hide();
  }
  </code></pre>