I want to make fun of Microsoft.Office.Interop.Excel.Range (and other Microsoft.Office.Interop.Excel interfaces) in the unit test of my application. I am using Moq 4.0.10827 with .NET 4 in C #.
In my unit test, I am trying to adjust the layout behavior as follows:
var range = new Mock<Microsoft.Office.Interop.Excel.Range>(); range.Setup(r => r.get_Value(1)).Returns("Something"); var classBeingTested = new MyClass(range.Object);
In the module under test, I use Range as follows:
public MyClass(Range range) { var value = range[1].ToString(); }
When a test runs, it throws the following exception:
Error: Missing method 'instance object [My.Example.Implementation.MyClass] Microsoft.Office.Interop.Excel.Range::get__Default(object,object)' from class 'Castle.Proxies.RangeProxy'.
How can I realize this mockery successfully? I understand that isolating the functionality of Excel Interop with a template such as https://stackoverflow.com/a/312616/ ... is a potential solution; however, I would be happy if I could create mocks from Microsoft.Office.Interop.Excel interfaces.
EDIT: More on this issue.
OK, this is strange. I created a project to test jimmy_keen's suggestions. It is working. However, when I use the same sentence to test a project that I had problems with, it throws the following exception:
Error: Missing method 'instance object [My.Example.Implementation.MyClass] Microsoft.Office.Interop.Excel.Range::get__Default(object,object)' from class 'Castle.Proxies.RangeProxy'.
Since the jimmy_keen proposal worked fine on other projects, I began to suspect that something was wrong with the project with which I am testing the code. So, I created a test solution that demonstrates the problem in detail: http://www7.zippyshare.com/v/70834394/file.html
The root of the problem, apparently, is that the project contains another class (which is not tested by the module) essentially using the following code:
using Microsoft.Office.Interop.Excel; namespace Project { public class UnTestedClass { public UnTestedClass(Worksheet sheet) { var foo = sheet.Range["Description"].Column; } } }
I do not understand why this is causing the problem, because I do not use UnTestedClass in unit test at all. Am I missing something about how I should use Moq, or did I come across an error?