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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:52:17+00:00 2026-05-11T13:52:17+00:00

I am attempting to mock a call to an indexed property. I.e. I would

  • 0

I am attempting to mock a call to an indexed property. I.e. I would like to moq the following:

object result = myDictionaryCollection['SomeKeyValue']; 

and also the setter value

myDictionaryCollection['SomeKeyValue'] = myNewValue; 

I am doing this because I need to mock the functionality of a class my app uses.

Does anyone know how to do this with MOQ? I’ve tried variations on the following:

Dictionary<string, object> MyContainer = new Dictionary<string, object>(); mock.ExpectGet<object>( p => p[It.IsAny<string>()]).Returns(MyContainer[(string s)]); 

But that doesn’t compile.

Is what I am trying to achieve possible with MOQ, does anyone have any examples of how I can do this?

  • 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. 2026-05-11T13:52:18+00:00Added an answer on May 11, 2026 at 1:52 pm

    It appears that what I was attempting to do with MOQ is not possible.

    Essentially I was attempting to MOQ a HTTPSession type object, where the key of the item being set to the index could only be determined at runtime. Access to the indexed property needed to return the value which was previously set. This works for integer based indexes, but string based indexes do not work.

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

Sidebar

Related Questions

I'm attempting to mock some file operations. In the real object I have: StreamWriter
Attempting to use asp.net mvc's Action Result of File. So it would seem that
I've got the following line, attempting to create a mock of a concrete type:
I am attempting to Mock an IDataRecord interface. So far I have: var mockIDataRecord
I have a device and the drivers for this device. What I would like
I have the following setup code: MockOf<IObjectSet<Dummy>>().Setup(c => c.AddObject(dummy)).Verifiable(); MockOf<IObjectContextWrapper>().Setup(c => c.GetObjectSet<Dummy>()).Returns(MockOf<IObjectSet<Dummy>>().Object); where Dummy
I am attempting to use PHPunit to mock out some dependencies, but my mock
I'm attempting to test my repository using an in-memory mock context. I implement this
I am attempting to utilize Moq for my unit tests. I added the library
When mocking a call to a WCF Service, I get the following error: Method

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.