locked
How to do In-Memory repository unit test when working with EF6 with asp.net mvc 4/5 RRS feed

  • Question

  • User1183902823 posted

    suppose this is my repository

    public interface IUserRepository:IDisposable
        {
            IEnumerable<User> GetUsers();
            User GetUserByID(int userId);
            void InsertUser(User user);
            void DeleteUser(int userId);
            void UpdateUser(User user);
            void Save();
        }
    public class UserRepository:IUserRepository
        {
            private MVCEntities context;
    
            public UserRepository(MVCEntities context)
            {
                this.context = context;
            }
    
            public IEnumerable<User> GetUsers()
            {
                return context.Users.ToList();
            }
    
            public User GetUserByID(int userId)
            {
                return context.Users.Find(userId);
            }
    
            public void InsertUser(User user)
            {
                context.Users.Add(user);
            }
    
            public void DeleteUser(int userId)
            {
                User user = context.Users.Find(userId);
                context.Users.Remove(user);
            }
    
            public void UpdateUser(User user)
            {
                context.Entry(user).State = EntityState.Modified;
            }
    
            public void Save()
            {
                context.SaveChanges();
            }
    
            private bool disposed = false;
    
            protected virtual void Dispose(bool disposing)
            {
                if (!this.disposed)
                {
                    if (disposing)
                    {
                        context.Dispose();
                    }
                }
                this.disposed = true;
            }
    
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
        }

    please provide me guidance with code example that how to do In-Memory repository unit test when working with EF6 with asp.net mvc 4/5.

    why people choose sql lite or sql CE as database for EF in-memory test ?
    why not they choose sql server local db which reside in app_data folder for EF in-memory test ?

    thanks

    Tuesday, December 19, 2017 11:52 AM

Answers

All replies

  • User617292609 posted

    You need to start using mocking frameworks. Moq is a good one:

    https://www.nuget.org/packages/Moq/

    https://github.com/Moq/moq4/wiki/Quickstart

    Here's also an article that might be useful: https://msdn.microsoft.com/en-us/library/dn314429(v=vs.113).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 20, 2017 6:57 AM
  • User1400794712 posted

    Hi tridip1974,

    1) About testing with InMemory, please refer to this article.

    https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/in-memory 

    2) SQLite has an in-memory model that allows us to use SQLite to write tests against a relational database, without the overhead of actual database operations. So, it's convenient to use SQLite. Actually, we can also use local db for testing, please refer to this article:

    https://guyharwood.co.uk/2013/03/30/entity_framework_tests_with_local_db/ 

    Best Regards,

    Daisy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 20, 2017 7:44 AM
  • User1183902823 posted
    @daisy i have one request. I am not a advance developer. here i have given a code for my repository.So would you post code to unit test my repo with in-memory approach. I am using asp.net mvc v4/5.

    i saw articles exit for EF core in-memory test but i found none for EF6.
    Wednesday, December 20, 2017 4:23 PM
  • User1183902823 posted

    @daisy

    i checked your url for in-memory testing.

    see this code

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFProviders.InMemory;Trusted_Connection=True;ConnectRetryCount=0");
        }
    }

    what is the meaning of the above line? are they using sql server for in-memory test? are they using local db?

    [TestMethod]
            public void Add_writes_to_database()
            {
                var options = new DbContextOptionsBuilder<BloggingContext>()
                    .UseInMemoryDatabase(databaseName: "Add_writes_to_database")
                    .Options;
    
                // Run the test against one instance of the context
                using (var context = new BloggingContext(options))
                {
                    var service = new BlogService(context);
                    service.Add("http://sample.com");
                }
    
                // Use a separate instance of the context to verify correct data was saved to database
                using (var context = new BloggingContext(options))
                {
                    Assert.AreEqual(1, context.Blogs.Count());
                    Assert.AreEqual("http://sample.com", context.Blogs.Single().Url);
                }
            }

    what is the meaning of this line databaseName: "Add_writes_to_database"

    what is the meaning of this line

                    var service = new BlogService(context);
                    service.Add("http://sample.com");

    thanks

    Thursday, December 21, 2017 10:45 AM