title: How do I avoid using dynamic when mocking an Excel.worksheet?
body: <p>I'm trying to mock an Excel spreadsheet using NSubstitute or other mocking framework and MSTest (Visual Studio 2010). I'm not sure if there's a better way than this--and this doesn't quite work for testing:</p>
  
  <p>Here's an example (this is all prototype code right now, and not very clean):</p>
  
  <pre><code>int[] lowerBounds = { 1, 1 };
  int[] lengths = { 2, 2 };
  
  //Initialize a 1-based array like Excel does:
  object[,] values = (object[,])Array.CreateInstance(typeof(object), lengths, lowerBounds);
  values[1,1] = "hello";
  values[2,1] = "world";      
  
  //Mock the UsedRange.Value2 property
  sheet.UsedRange.Value2.Returns(values); 
  
  //Test:   
  GetSetting(sheet, "hello").Should().Be("world");  //FluentAssertions
  </code></pre>
  
  <p>So far, so good: this passes if the GetSetting method is <b>in the same project</b> as my test. However when GetSetting is in my VSTO Excel-Addin project, it fails with the following error on the first line of the GetSetting function:</p>
  
  <pre><code>System.MissingMethodException: Error: Missing method 'instance object [MyExcel.AddIn] Microsoft.Office.Interop.Excel.Range::get_Value2()' from class 'Castle.Proxies.RangeProxy'.
  </code></pre>
  
  <p>For reference, the GetSetting grabs a value from columnA in the sheet, and returns the value in columnB.</p>
  
  <pre><code>public static string GetSetting(Excel.Worksheet sheet, string settingName) {
    object[,] value = sheet.UsedRange.Value2 as object[,];
    for (int row = 1; row &lt;= value.GetLength(1); row++) {
      if (value[1, row].ToString() == settingName)
        return value[2, row].ToString();
    }
    return "";
  }
  </code></pre>
  
  <p>The final interesting piece is if I redefine the signature of my method as follows:<br>
  <em>public static string GetSetting(<b>dynamic</b> sheet, string settingName)</em><br>
  it works in the VSTO project.</p>
  
  <p>So what is going on, and what's the best way to do something like this?</p>
  
  <p>Thanks!</p>