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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:36:56+00:00 2026-06-03T21:36:56+00:00

I got a problem with Moq and Automapper regarding IDataReader. I found an example

  • 0

I got a problem with Moq and Automapper regarding IDataReader.
I found an example on stackoverflow and modified the code.

   public static IDataReader MockIDataReader<T>(List<T> ojectsToEmulate) where T : class
    {
        var moq = new Mock<IDataReader>();

        // This var stores current position in 'ojectsToEmulate' list
        var count = 0;

        moq.Setup(x => x.Read())
            // Return 'True' while list still has an item
            .Returns(() => count < ojectsToEmulate.Count)
            // Go to next position
            .Callback(() => count++);

        var properties = typeof (T).GetProperties();
        foreach (PropertyInfo t in properties)
        {
            var propName = t.Name;
            moq.Setup(x => x[propName]).Returns(() => ojectsToEmulate[count].GetType().GetProperty(propName).GetValue(ojectsToEmulate[count],null));
        }
        return moq.Object;
    }
}

My mapping:

Mapper.Configuration.CreateMap(typeof(IDataReader), typeof(IEnumerable<T>));
var result = Mapper.Map<IDataReader, IEnumerable<T>>(reader);

The problem I got here is that my result has 1 result a cityModel but all it’s properties are null. If I check the value from my mocked reader like reader[“name”] I got the “Alingsås” value so the mocking is correct but Automapper seams to have the problem.
I use a List of objects that I pass to my method that mocks it all.

var cityModel = new CityModel();
cityModel.Name = "Alingsås";
cityModel.Id = "SE";
cityModel.CountryId = "SE";

var cityModels = new List<CityModel>();
cityModels.Add(cityModel);

 _fakeReader = MockTester.MockIDataReader(cityModels);

The code works fine, no exception is thrown, but the mapper gives me an object without the
valules. I can see in the debugger my reflection code works but it seams like my
x[“Name”] aren’t the method Automapper call from IDataReader? Or is it?

What can be wrong here?

  • 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-03T21:36:58+00:00Added an answer on June 3, 2026 at 9:36 pm

    Automapper internally uses the int indexer of IDataReader so you need to call Setup on that instead of Item[String].

    Checking the Automapper’s source you need to setup some additional methods to make it work:

    //...
    var index = 0;
    foreach (PropertyInfo t in properties)
    {
        var propName = t.Name;
        int index1 = index; // avoid access to modified closure
        moq.Setup(x => x.GetFieldType(index1)).Returns(t.PropertyType);
        moq.Setup(x => x.GetName(index1)).Returns(propName);
        moq.Setup(x => x[index1])
            .Returns(ojectsToEmulate[count]
                     .GetType()
                     .GetProperty(propName).GetValue(ojectsToEmulate[count], null));
        index++;
    }
    
    moq.Setup(x => x.FieldCount).Returns(properties.Length);
    //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got problem with sending data in Android using httpPost. I found some example,
I have got problem with this method: public List<int> menu_wid_w_kat() { DataSet1TableAdapters.menu_widac_wszystkoTableAdapter pk =
i got problem with my code and hopefully someone able to figure it out.
I want to ask about strcpy. I got problem here. Here is my code:
Hello I got problem with scroll on my grid. Here is the code (nothing
I've got problem with this peace of code, it should change lower case letters
I fully understand the use of list in lisp but I 've got problem
Hi I've got problem with foreach statement. I have my controller method: public ActionResult
I'm trying configure AzMan but I Have got problem with code. Can you explain
I'm trying to implement a linked list class in C++ and I got problem.

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.