locked
Unit test with generic repository, unit of work and service layer RRS feed

  • Question

  • Hi

    I have a MVC app and need to add unit testing.

    The layers i have is

    • MVC app
    • Service Layer
    • Repository / Unit of work
    • Data layer

    I have a IClientService:

    public interface IClientService
        {
            Client GetClient(int clientId);
        }

    ClientService:

    public class ClientService : IClientService
        {
            private UnitOfWork uow = new UnitOfWork();
    
            public Client GetClient(int clientId)
            {
                Client client = uow.ClientRepository.GetByID(clientId);
                return client;
            }
        }

    My Repository:

     public interface IRepository<TEntity> where TEntity : class
        {
            IEnumerable<TEntity> Get(
                Expression<Func<TEntity, bool>> filter = null,
                Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
                string includeProperties = "");
    
            TEntity GetByID(object id);
            void Insert(TEntity entity);
            void Delete(object id);
            void Delete(TEntity entityToDelete);
            void Update(TEntity entityToUpdate);
        }
    
    
    public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
        {
            internal DSModelContainer context;
            internal DbSet<TEntity> dbSet;
    
            public Repository(DSModelContainer context)
            {
                this.context = context;
                this.dbSet = context.Set<TEntity>();
            }
    
            public virtual IEnumerable<TEntity> Get(
                Expression<Func<TEntity, bool>> filter = null,
                Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
                string includeProperties = "")
            {
                IQueryable<TEntity> query = dbSet;
    
                if (filter != null)
                {
                    query = query.Where(filter);
                }
    
                foreach (var includeProperty in includeProperties.Split
                    (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    query = query.Include(includeProperty);
                }
    
                if (orderBy != null)
                {
                    return orderBy(query).ToList();
                }
                else
                {
                    return query.ToList();
                }
            }
    
            public virtual TEntity GetByID(object id)
            {
                return dbSet.Find(id);
            }
    
            public virtual void Insert(TEntity entity)
            {
                dbSet.Add(entity);
               
            }
    
            public virtual void Delete(object id)
            {
                TEntity entityToDelete = dbSet.Find(id);
                Delete(entityToDelete);
            }
    
            public virtual void Delete(TEntity entityToDelete)
            {
                if (context.Entry(entityToDelete).State == EntityState.Detached)
                {
                    dbSet.Attach(entityToDelete);
                }
                dbSet.Remove(entityToDelete);
            }
    
            public virtual void Update(TEntity entityToUpdate)
            {
                dbSet.Attach(entityToUpdate);
                context.Entry(entityToUpdate).State = EntityState.Modified;
            }
    
        }


    And last my unitOfWork:

    public interface IUnitOfWork
        {    
            Repository<Client> ClientRepository { get; }
        
            void Save();
        }
    
    
     public class UnitOfWork : IUnitOfWork
        {
            public DSModelContainer context = new DSModelContainer();
    
                    private Repository<Client> clientRepository;  
            public Repository<Client> ClientRepository
            {
                get
                {
    
                    if (this.clientRepository == null)
                    {
                        this.clientRepository = new Repository<Client>(context);
                    }
                    return clientRepository;
                }
            }
        }

    And then my question is, how do i unit test my appliction?

    I have been reading about testing repository, but was not able to do it with my architecture, and im really stock..

    Hope someone can help

    Thanks



    • Edited by Jako9225 Thursday, May 8, 2014 9:20 AM
    Thursday, May 8, 2014 9:18 AM

Answers

  • Hi,

    Visual Studio Unit Test is used to verify whether a method has logic errors.

    When you create a new ASP.NET MVC project in Visual Studio, the Create Unit Test Project dialog box is displayed. If you select Yes and create unit tests, a test project will be created in your ASP.NET MVC solution that contains unit tests for the account controller and the home controller. These test classes provide a good introduction to MVC unit testing.

    ASP.NET MVC unit tests directly call methods of your MVC controllers. When a unit test calls an action method in a controller, you can validate that the correct view is returned (although you do not validate the HTML) and that view data is returned. You can also test whether a method correctly redirects to another controller or view.

    As for how to unit test for MVC applications, you can start from here: Unit Testing in ASP.NET MVC Applications Those articles also provided detailed code samples for learning.

    If you want to unit test repository, unit of work, you need know what you test, it is a method.

    As for how to unit test a repository, please reference this thread with similar requirements:

    http://social.msdn.microsoft.com/Forums/en-US/8e728a9a-13b1-4f7d-baee-7e77204f7371/unit-testing-a-repository?forum=vsunittest

    David shared a detailed solution and code.

    This article from CodePlex also provides detailed steps and code samples to unit test for repository and unit of work, reference: http://www.codeproject.com/Articles/579035/Creating-Unit-Testable-Applications-in-ASP-NET-MVC

    About how to unit test service layer, this thread should help you: http://stackoverflow.com/questions/11530279/need-help-to-unit-test-my-service-layer-in-my-mvc3-project


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Amanda Zhu Wednesday, May 14, 2014 1:21 AM
    Friday, May 9, 2014 7:39 AM

All replies