Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to mock Microsoft.Office.Interop.Excel.Range with Moq?
    primarykey
    data
    text
    <p>I want to mock the Microsoft.Office.Interop.Excel.Range (and the other Microsoft.Office.Interop.Excel interfaces) to unit test my application. I'm using Moq 4.0.10827 with .NET 4 in C#.</p> <p>In my unit test, I attempt to set up the behavior of the mock as follows:</p> <pre><code> var range = new Mock&lt;Microsoft.Office.Interop.Excel.Range&gt;(); range.Setup(r =&gt; r.get_Value(1)).Returns("Something"); var classBeingTested = new MyClass(range.Object); </code></pre> <p>In the code being unit tested, I'm using the Range as follows:</p> <pre><code> public MyClass(Range range) { var value = range[1].ToString(); } </code></pre> <p>When the test runs, it produces the following exception:</p> <pre><code> Error: Missing method 'instance object [My.Example.Implementation.MyClass] Microsoft.Office.Interop.Excel.Range::get__Default(object,object)' from class 'Castle.Proxies.RangeProxy'. </code></pre> <p>How can I implement this mocking successfully? I realize that isolating the Excel Interop functionality with a pattern such as <a href="https://stackoverflow.com/a/9486226/1548950">https://stackoverflow.com/a/9486226/1548950</a> is a potential solution; however, I'd be happier with being able to create mocks from the Microsoft.Office.Interop.Excel interfaces.</p> <p><strong>EDIT: More details on this issue</strong></p> <p>OK, this is strange. I've created a project to test the suggestion by jimmy_keen. It works. However, when I use the same suggestion to test the project I had issues with, it throws the following exception:</p> <pre><code>Error: Missing method 'instance object [My.Example.Implementation.MyClass] Microsoft.Office.Interop.Excel.Range::get__Default(object,object)' from class 'Castle.Proxies.RangeProxy'. </code></pre> <p>Since jimmy_keen's suggestion worked fine on other projects, I started suspecting that there is something wrong with the project I'm testing the code with. So, I created a test solution that demonstrates the problem in detail: <a href="http://www7.zippyshare.com/v/70834394/file.html" rel="nofollow noreferrer">http://www7.zippyshare.com/v/70834394/file.html</a></p> <p>The root of the problem seems to be that the project contains another class (that is not being unit tested) with essentially the following code:</p> <pre><code>using Microsoft.Office.Interop.Excel; namespace Project { public class UnTestedClass { public UnTestedClass(Worksheet sheet) { var foo = sheet.Range["Description"].Column; } } } </code></pre> <p>I don't understand why this causes the issue, because I'm not using UnTestedClass in the unit test at all. Am I missing something in how I should use Moq, or have I stumbled upon a bug?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload