Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8572743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:05:40+00:00 2026-06-11T19:05:40+00:00

I’m trying to mock an Excel spreadsheet using NSubstitute or other mocking framework and

  • 0

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:

Here’s an example (this is all prototype code right now, and not very clean):

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

So far, so good: this passes if the GetSetting method is in the same project 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:

System.MissingMethodException: Error: Missing method 'instance object [MyExcel.AddIn] Microsoft.Office.Interop.Excel.Range::get_Value2()' from class 'Castle.Proxies.RangeProxy'.

For reference, the GetSetting grabs a value from columnA in the sheet, and returns the value in columnB.

public static string GetSetting(Excel.Worksheet sheet, string settingName) {
  object[,] value = sheet.UsedRange.Value2 as object[,];
  for (int row = 1; row <= value.GetLength(1); row++) {
    if (value[1, row].ToString() == settingName)
      return value[2, row].ToString();
  }
  return "";
}

The final interesting piece is if I redefine the signature of my method as follows:
public static string GetSetting(dynamic sheet, string settingName)
it works in the VSTO project.

So what is going on, and what’s the best way to do something like this?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:05:42+00:00Added an answer on June 11, 2026 at 7:05 pm

    The VS2012 update: Moq & Interop Types: works in VS2012, fails in VS2010?

    First up: Something has changed: How do I avoid using dynamic when mocking an Excel.worksheet?

    I encountered the same problem Mocking Excel objects using NSubstitute. The dynamic resolved the problem just as you mention. However I wanted to find the root cause.


    When your Project has a reference to Microsoft.Office.Interop.Excel.Extensions.dll you need to check if the Embed Interop Types property is visible. If it is that means your targeting .Net 4.0 (which I could guess from the dynamic keyword).

    You can leave the Test Project targeting .Net 4.0 but you need to you
    change the VSTO Project .Net framework back to 3.5. Then you’ll
    probably have to do some explicit casting and fully qualify things to
    get rid of these errors:

    C# Office Excel Interop “object does not contain definition for” errors, here are a couple of examples:

    .Net 4.0:

    if (tmpsheetName == xlApp.ActiveSheet.Name)
    

    .Net 3.5 equivalent

    Worksheet activeSheet = (Worksheet)xlApp.ActiveSheet;
    if (tmpsheetName == activeSheet.Name)
    

    Another example:

    rn.Select();
    

    .Net 4.0

    xlApp.Selection.HorizontalAlignment = Constants.xlCenter; 
    xlApp.Selection.Font.Bold = true;
    xlApp.Selection.Merge();
    

    .Net 3.5 equivalent

    rn.HorizontalAlignment = Constants.xlCenter;
    rn.Font.Bold = true;
    rn.Merge();
    

    Proceed to fix all the .Net 3.5 vs 4.0 syntax errors as per the above examples. Dont forget to remove the dynamic parameter type and replace it the original Worksheet. Finally fire up the Test again and it will pass!!!

    Given all the grief I experienced with Microsoft.CSharp.DLL in this thread I am of the opinion testing VSTO .Net 4.0 projects with Mocking Frameworks doesn’t work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.