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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:21:09+00:00 2026-05-27T16:21:09+00:00

We have a scenario in our unit tests where we have created a FakeDbSet

  • 0

We have a scenario in our unit tests where we have created a FakeDbSet that implements IDbSet. In a FakeUnitOfwork I have properties that are IDbSets and getting new-ed up using the FakeDbSet.

Without having to write out each different property in my Commit method I am trying to use reflection to iterate over the properties inside the FakeUnitOfWork. I then want to copy the property values to a differnt class that has propertes of List<> of the same type. So I may have a property in my FakeUnitOfWork:

IDbSet<User> Users {get {return _users ?? (_users = new FakeDbSet<User>());}

In my fake data store I have this property:

List<User> Users {get;set;}

This is hwat I have so far:

public void Commit()
{ 
     foreach (var property in typeof(TestUnitOfWork).GetProperties())
            {
               var testContextType = typeof (TestDataContext).GetProperty(property.Name);
//I then want to do a ToList() on the TestUnitOfWork IDbSet properties to push them into the TestDataContext.


            }
}

So, I am not sure how to know I am looking at, say, a IDbSet (from FakeUnitOfWork) and a List (from my fake memory data store) so that I can copy the data from FakeUnitOfWork over to the data store. Since they have the same names I only need to figure out how to do the casting via reflection.

Update: I tried something like this and thought I might be on the irght track, but the code never gets hit:

foreach (var property in typeof(TestUnitOfWork).GetProperties())
                {
                   var testContextType = typeof (TestDataContext).GetProperty(property.Name);
                   if(property.GetValue(this,null) is IDbSet<MyBaseEntityType>)
                   {
                        testContextType.SetValue(TestDataContext, ((IDbSet<MyBaseEntityType>) property.GetValue(this,null)).ToList(),null);
                   }


                }
  • 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-05-27T16:21:10+00:00Added an answer on May 27, 2026 at 4:21 pm

    You used a reflection to get types and properties for your context and unit of work. Now you have property of type List<> on one side and you want to assign content of property of type IDbSet<> to it. Is it right?

    The theory

    To do that you need to call ToList method on IDbSet<> but this method is not part of IDbSet<> interface. It is extension method defined in System.Linq.Enumerable static class. Extension method is just static method with syntactic sugar but it can still be called as normal static method. So you must lookup Enumerable class using reflection (get type), get generic method info for ToList method and target it to proper generic argument (used by current IDbSet<>). Then you can pass your set as parameter to this method and invoke it.

    Anyway better approach would be avoiding as many reflection as possible. For example expose special interface on TestUnitOfWork which would offer direct access to lists.

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

Sidebar

Related Questions

Our scenario: We have a main database that stores company-wide information. We have several
In one of my Django projects I have a suite of unit tests that
we have following scenario for our new project: 1) Organization have central office where
Here's our scenario: We have an MVC3 Application 'MVC3ABC' in solution S1 that is
I have some written a number of unit tests that test a wrapper around
We have an old asp.net application that has no unit tests, integration tests, component
I have a scenario where I am to send email using our own SMTP
I have a path defined: when /the admin home\s?page/ /admin/ I have scenario that
I have not used Junit before and have not done unit testing automatically. Scenario:
We have a scenario where we wish to analyse the visitors to our web

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.