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 8267819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:40:21+00:00 2026-06-08T05:40:21+00:00

I want to mock the Microsoft.Office.Interop.Excel.Range (and the other Microsoft.Office.Interop.Excel interfaces) to unit test

  • 0

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#.

In my unit test, I attempt to set up the behavior of the mock 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 code being unit tested, I’m using the Range as follows:

    public MyClass(Range range)
    {
        var value = range[1].ToString();
    }

When the test runs, it produces 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 implement this mocking successfully? I realize that isolating the Excel Interop functionality with a pattern such as https://stackoverflow.com/a/9486226/1548950 is a potential solution; however, I’d be happier with being able to create mocks from the Microsoft.Office.Interop.Excel interfaces.

EDIT: More details on this issue

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:

Error: Missing method 'instance object [My.Example.Implementation.MyClass] Microsoft.Office.Interop.Excel.Range::get__Default(object,object)' from class 'Castle.Proxies.RangeProxy'.

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: http://www7.zippyshare.com/v/70834394/file.html

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:

using Microsoft.Office.Interop.Excel;
namespace Project
{
    public class UnTestedClass
    {
        public UnTestedClass(Worksheet sheet)
        {
            var foo = sheet.Range["Description"].Column;
        }
    }
}

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?

  • 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-08T05:40:24+00:00Added an answer on June 8, 2026 at 5:40 am

    Your code accesses indexer, and that’s what you need to mock:

    var range = new Mock<Microsoft.Office.Interop.Excel.Range>();
    range.Setup(r => r[1, It.IsAny<object>()]).Returns("something");
    

    Note that Range indexer actually takes two parameters – hence the It.IsAny<object>() constraint in call.

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

Sidebar

Related Questions

I want to mock only the GetValue method of the following class, using Moq:
I'm trying to unit test my websocket on node.js and want to mock out
I want to mock Find method which expects a predicate using Moq: public PurchaseOrder
I want to mock ASP.NET 3.5 behavior in order to unit test my WebControls:
I want to mock this interface using Moq IInterfaceToBeMocked { IEnumerable<Dude> SearchDudeByFilter(Expression<Func<Dude,bool>> filter); }
I am using JustMock to mock interfaces for unit testing, but perhaps I'm not
I want to make a mock installer using NSIS so we can demo what
I want to mock a ServiceContract. The problem is that Moq (and Castle Dynamic-Proxy)
I want to start using mock objects on my c# project. After a quick
I want to mock the following in my action controller using RhinoMocks Session[myId] =

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.