I\'m mocking VSTO objects and in one project (I didn\'t write) it has this code:
var listOfSheets = new List();
var mockSheets = Substitute.
Some history:
I was getting a compile issue with these errors:
1.Predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported
2.One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?
So I found this article and referenced the Microsoft.CSharp library: C# 4.0 and .Net 3.5
To the point: The class I've been showing essentially Mocks the Excel Object Model and I copied this class from one project to the other (I cant reference the other project as its actually separate project plus it would have caused circular dependency) I found the Returns extension method was listed in intellisense but I got a "Could not resolve Symbol" when compiling. Even though going to Definition was the same in both classes/projects. To get around this initially I did the commented out lines:
public static Range Cell
{
get
{
var mockCell = Substitute.For();
mockCell.Address.Returns("$A$1");
mockCell.Formula = "=1+1";
mockCell.ToString().Returns(mockCell.Formula.ToString());
//mockCell.ToString().Returns(info => mockCell.Formula.ToString());
//SubstituteExtensions.Returns(mockCell.ToString(), mockCell.Formula.ToString());
mockCell.Worksheet.Returns(Sheet);
mockCell.Worksheet.Name.Returns(MockSheetName);
return mockCell;
}
}
This point is a bit of a red-herring but removing the Microsoft.CSharp dll actually allowed the Returns extension method to resolve successfully. Then I found removing the Microsoft.CSharp dll resolved my problem, it all just worked, the mockSheet object has all its properties and was able to execute successfully without the "Cannot perform runtime binding on a null reference" error.
Oh and a tip for anyone Mocking Interop Types, be extra careful to set this:
