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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:12:45+00:00 2026-06-17T13:12:45+00:00

My code looks as below: public class FileFormatLookup : Dictionary<string,Func<string>> { private Workbook workbook;

  • 0

My code looks as below:

public class FileFormatLookup : Dictionary<string,Func<string>>
{
    private Workbook workbook;
    private MemoryStream memoryStream;

    public  FileFormatLookup(Workbook workbook, MemoryStream memoryStream)
    {
        this.workbook = workbook;
        this.memoryStream = memoryStream;
        Add();
    }

    private void Add()
    {
        base.Add("xls", () =>
                            {
                                workbook.Save(memoryStream, SaveFormat.Excel97To2003);
                                return "ms-excel";
                            });
    }
}

And I am trying to write the test as below:

class FileFormatLookupTest
{
    FileFormatLookup fileFormatLookup;
    private Workbook workbook;
    private MemoryStream memoryStream;

    [SetUp]
    public void SetUp()
    {
        fileFormatLookup = new FileFormatLookup(workbook, memoryStream);
        workbook = MockRepository.GenerateMock<Workbook>();
        memoryStream = MockRepository.GenerateMock<MemoryStream>();
    }

    [TearDown]
    public void TearDown()
    {
      workbook.VerifyAllExpectations();
    }


    [Test]
    public void ShouldHaveXlsKey()
    {
        var xlsResult = fileFormatLookup["xls"].Invoke();
        workbook.AssertWasCalled(x=>x.Save(memoryStream,SaveFormat.Excel97To2003));
     }
}

Is this the correct way to test a functor inside dictionary?
It throws this error :

System.InvalidOperationException : No expectations were setup to be verified, ensure that the method call in the action is a virtual (C#) / overridable (VB.Net) method call

How do I test a dictionary which returns a function? Is it possible?
I can assert the string value which is returned but not the method calls.

  • 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-17T13:12:46+00:00Added an answer on June 17, 2026 at 1:12 pm

    You are not passing your mocks to the class being tested.

    Change setup to

    [SetUp]
    public void SetUp()
    {
        workbook = MockRepository.GenerateMock<Workbook>();
        memoryStream = MockRepository.GenerateMock<MemoryStream>();
        fileFormatLookup = new FileFormatLookup(workbook, memoryStream);
    }
    

    From the error message, it looks like to need to set up expectations, i.e. which calls on the mock objects you are expecting. In this case workbook.Save(...).

    To do this, workbook.Save(...) must be either virtual or on an interface.


    Edit: after further information provided as a comment.

    If the workbook.Save(...) is not virtual and not within your code, you must go a slightly longer route to be able to test the object.

    Create your own IWorkbook interface which implements the Save() method, then a WorkbookWrapper class which contains an instance of the 3rd party Workbook class and implements IWorkbook by forwarding calls to the wrapped class.

    public interface IWorkbook
    {
        void Save(...);
    }
    
    class WorkbookWrapper : IWorkbook
    {
        private _workbook;
    
        public WorkbookWrapper(...)
        {
            _workbook = new Workbook(...);
        }
    
        public void Save(...)
        {
            _workbook.Save(...);
        }
    }
    

    Then change your code to take an IWorkbook instead:

    private Workbook _workbook;
    
    public FileFormatLookup(IWorkbook workbook, MemoryStream memoryStream)
    {
        _workbook = workbook;
        _memoryStream = memoryStream;
        Add();
    }
    

    You could then use the same technique for other classes, e.g. the MemoryStream.

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

Sidebar

Related Questions

The code looks like below: Clock: public class Clock { public event Func<DateTime, bool>
My code looks like below: private void LayoutRoot_Loaded(object sender, System.Windows.RoutedEventArgs e) { // TODO:
I have a data that looks like this . And my code below simply
Today I noticed a snippet of code that looks like the one below: public
I have a MyClassFile.cs file that looks like this: public class MyClass1 { public
The code looks like below: namespace Test { public interface IMyClass { List<IMyClass> GetList();
I have code that looks more or less like the code below but it
I have the code below. It looks long and complicated and now I have
Look please below this codes throw me : FormatException was unhandled by user code
Please have a look at the code below: import string from collections import defaultdict

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.