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

  • Home
  • SEARCH
  • 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 8782469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:34:01+00:00 2026-06-13T20:34:01+00:00

I’m using entity framework and trying to unit test my data services which are

  • 0

I’m using entity framework and trying to unit test my data services which are using EF.
I’m not using repository and unit of work patterns.
I tried the following approach to mock the context and DbSet:

private static Mock<IEFModel> context;
private static Mock<IDbSet<CountryCode>> idbSet;

    [ClassInitialize]
    public static void Initialize(TestContext testContext)
    {
        context = new Mock<IEFModel>();

        idbSet = new Mock<IDbSet<CountryCode>>();

        context.Setup(c => c.CountryCodes).Returns(idbSet.Object);

    }

I get null “Object reference not set to an instance of an object” error for idbSet “Local”.
Is there any way to mock idbSet like this?
Thanks

  • 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-13T20:34:02+00:00Added an answer on June 13, 2026 at 8:34 pm

    I worked it out like this:
    Created two classes named DbSetMock:

    public class DbSetMock<T> : IDbSet<T>
        where T : class
    {
        #region Fields
    
        /// <summary>The _container.</summary>
        private readonly IList<T> _container = new List<T>();
    
        #endregion
    
        #region Public Properties
    
        /// <summary>Gets the element type.</summary>
        public Type ElementType
        {
            get
            {
                return typeof(T);
            }
        }
    
        /// <summary>Gets the expression.</summary>
        public Expression Expression
        {
            get
            {
                return this._container.AsQueryable().Expression;
            }
        }
    
        /// <summary>Gets the local.</summary>
        public ObservableCollection<T> Local
        {
            get
            {
                return new ObservableCollection<T>(this._container);
            }
        }
    
        /// <summary>Gets the provider.</summary>
        public IQueryProvider Provider
        {
            get
            {
                return this._container.AsQueryable().Provider;
            }
        }
    
        #endregion
    
        #region Public Methods and Operators
    
        /// <summary>The add.</summary>
        /// <param name="entity">The entity.</param>
        /// <returns>The <see cref="T"/>.</returns>
        public T Add(T entity)
        {
            this._container.Add(entity);
            return entity;
        }
    
        /// <summary>The attach.</summary>
        /// <param name="entity">The entity.</param>
        /// <returns>The <see cref="T"/>.</returns>
        public T Attach(T entity)
        {
            this._container.Add(entity);
            return entity;
        }
    
        /// <summary>The create.</summary>
        /// <typeparam name="TDerivedEntity"></typeparam>
        /// <returns>The <see cref="TDerivedEntity"/>.</returns>
        /// <exception cref="NotImplementedException"></exception>
        public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, T
        {
            throw new NotImplementedException();
        }
    
        /// <summary>The create.</summary>
        /// <returns>The <see cref="T"/>.</returns>
        /// <exception cref="NotImplementedException"></exception>
        public T Create()
        {
            throw new NotImplementedException();
        }
    
        /// <summary>The find.</summary>
        /// <param name="keyValues">The key values.</param>
        /// <returns>The <see cref="T"/>.</returns>
        /// <exception cref="NotImplementedException"></exception>
        public T Find(params object[] keyValues)
        {
            throw new NotImplementedException();
        }
    
        /// <summary>The get enumerator.</summary>
        /// <returns>The <see cref="IEnumerator"/>.</returns>
        public IEnumerator<T> GetEnumerator()
        {
            return this._container.GetEnumerator();
        }
    
        /// <summary>The remove.</summary>
        /// <param name="entity">The entity.</param>
        /// <returns>The <see cref="T"/>.</returns>
        public T Remove(T entity)
        {
            this._container.Remove(entity);
            return entity;
        }
    
        #endregion
    
        #region Explicit Interface Methods
    
        /// <summary>The get enumerator.</summary>
        /// <returns>The <see cref="IEnumerator"/>.</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this._container.GetEnumerator();
        }
    
        #endregion
    }
    

    and EFModelMock:

    public class EFModelMock : IEFModel
    {
        #region Fields
    
        /// <summary>The country codes.</summary>
        private IDbSet<CountryCode> countryCodes;
    
        #endregion
    
        #region Public Properties
    
        /// <summary>Gets the country codes.</summary>
        public IDbSet<CountryCode> CountryCodes
        {
            get
            {
                this.CreateCountryCodes();
                return this.countryCodes;
            }
        }
    
    
        #endregion
    
        #region Public Methods and Operators
    
        /// <summary>The commit.</summary>
        /// <exception cref="NotImplementedException"></exception>
        public void Commit()
        {
            throw new NotImplementedException();
        }
    
        /// <summary>The set.</summary>
        /// <typeparam name="T"></typeparam>
        /// <returns>The <see cref="IDbSet"/>.</returns>
        /// <exception cref="NotImplementedException"></exception>
        public IDbSet<T> Set<T>() where T : class
        {
            throw new NotImplementedException();
        }
    
        #endregion
    
        #region Methods
    
        /// <summary>The create country codes.</summary>
        private void CreateCountryCodes()
        {
            if (this.countryCodes == null)
            {
                this.countryCodes = new DbSetMock<CountryCode>();
                this.countryCodes.Add(
                    new CountryCode { CountryName = "Australia", DisplayLevel = 2,       TelephoneCode = "61" });
    
            }
        }
    
        #endregion
    }
    

    Then tested like this:

    [TestClass]
    public class CountryCodeServiceTest
    {
        #region Static Fields
    
        /// <summary>The context.</summary>
        private static IEFModel context;
    
        #endregion
    
        #region Public Methods and Operators
    
        /// <summary>The initialize.</summary>
        /// <param name="testContext">The test context.</param>
        [ClassInitialize]
        public static void Initialize(TestContext testContext)
        {
            context = new EFModelMock();
        }
    
        /// <summary>The country code service get country codes returns correct data.</summary>
        [TestMethod]
        public void CountryCodeServiceGetCountryCodesReturnsCorrectData()
        {
            // Arrange
            var target = new CountryCodeService(context);
            var countryName = "Australia";
            var expected = context.CountryCodes.ToList();
    
            // Act
            var actual = target.GetCountryCodes();
    
            // Assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(actual.FirstOrDefault(a => a.CountryName == countryName).PhoneCode, expected.FirstOrDefault(a => a.CountryName == countryName).TelephoneCode);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:

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.