Moq-无法从IGenericRepository转换为Generic Repository

时间:2018-09-20 21:21:08

标签: c# unit-testing moq

请提前原谅我,我正在尝试学习单元测试和起订量。

我认为我几乎可以正确地进行设置了(但是我可能错了)。当我执行以下测试时,出现以下错误:

  

无法从转换   “ TestMoq.DAL.IGenericRepository”到   'TestMoq.DAL.GenericRepository'TestMoq.UnitTests

在这一行:

    _mockUnitWork.Setup(m => m.CreditRepository).Returns(_mockRepository.Object);

这是我的代码:

MoqTest

using System;
using NUnit.Framework;
using TestMoq.Controllers;
using TestMoq.Models;
using TestMoq.DAL;
using Moq;
using System.Web.Mvc;
using System.Runtime;
using System.Data.Entity;
using System.Collections.Generic;
using System.Linq;

namespace TestMoq.UnitTests.Controllers
{
    [TestFixture]
    public class BaseControllerTests
    {

        private Mock<IGenericRepository<Credit>> _mockRepository;
        Mock<IUnitOfWork> _mockUnitWork;



        [Test]
        public void MoqTest() {

            _mockRepository = new Mock<IGenericRepository<Credit>>();
            _mockUnitWork = new Mock<IUnitOfWork>();
            var _service = new CreditsController(_mockUnitWork.Object);

            List<Credit> listCredit= new List<Credit>() {
           new Credit() { CreditId = 1},
           new Credit() { CreditId = 2},
           new Credit() { CreditId = 3 }
          };

            _mockRepository.Setup(x => x.Get(null, null, "*")).Returns(listCredit);
            _mockUnitWork.Setup(m => m.CreditRepository).Returns(_mockRepository.Object);

            List<Credit> results = _service.GetAll() as List<Credit>;


            Assert.IsNotNull(results);
            Assert.AreEqual(3, results.Count);
        }
    }
}

CreditController.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TestMoq.CustomFilters;
using TestMoq.DAL;
using TestMoq.Models;

namespace TestMoq.Controllers
{
    public class CreditsController : Controller
    {
        private UnitOfWork unitOfWork = new UnitOfWork();
        private IUnitOfWork _unitOfWork;

        public CreditsController()
        {
            this._unitOfWork = new UnitOfWork();
        }
        public CreditsController(IUnitOfWork _NewUnitOfWork)
        {
            this._unitOfWork = _NewUnitOfWork;
        }       

        public List<Credit> GetAll()
        {
            return _unitOfWork.CreditRepository.Get().ToList();
        }

    }
}

GenericRepository

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using TestMoq.Models;

namespace TestMoq.DAL
{

    public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
    {
        internal TcpContext context;
        internal DbSet<TEntity> dbSet;

        public GenericRepository(TcpContext 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;
        }
    }
}

IGenericRepository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace TestMoq.DAL
{
    public interface IGenericRepository<TEntity> where TEntity : class
    {
        void Delete(object id);
        void Delete(TEntity entityToDelete);
        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 Update(TEntity entityToUpdate);
    }
}

UnitOfWork

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestMoq.Models;

namespace TestMoq.DAL
{

    public class UnitOfWork : IDisposable, IUnitOfWork
    {
        private TcpContext context = new TcpContext();
        public GenericRepository<Credit> creditRepository;

        //Other models & methods removed for clarity


        public GenericRepository<Credit> CreditRepository
        {
            get
            {
                if (this.creditRepository == null)
                {
                    this.creditRepository = new GenericRepository<Credit>(context);
                }

                return creditRepository;
            }

        }

        public void Save()
        {
            context.SaveChanges();
        }

    }
}

IUnitOfWork.cs

using TestMoq.Models;

namespace TestMoq.DAL
{
    public interface IUnitOfWork
    {
        GenericRepository<Amendment> AmendmentRepository { get; }
        GenericRepository<Credit> CreditRepository { get; }
        GenericRepository<FileUpload> FileUploadRepository { get; }
        GenericRepository<Job> JobRepository { get; }
        GenericRepository<Subscription> SubscriptionRepository { get; }
        GenericRepository<Topic> TopicRepository { get; }
        GenericRepository<Writer> WriterRepository { get; }

        void Dispose();
        void Save();
    }
}

如果我完全搞砸了,请提前道歉,如果您注意到我的思维方式中有其他重大错误(我仍然会全神贯注),请务必向我指出。

谢谢!

1 个答案:

答案 0 :(得分:0)

您只能模拟接口 如果您需要在IUnitOfWork中返回IGenericRepository。

类似的东西

public interface IUnitOfWork
    {
        IGenericRepository<Amendment> AmendmentRepository { get; }
        IGenericRepository<Credit> CreditRepository { get; }
        IGenericRepository<FileUpload> FileUploadRepository { get; }
        IGenericRepository<Job> JobRepository { get; }
        IGenericRepository<Subscription> SubscriptionRepository { get; }
        IGenericRepository<Topic> TopicRepository { get; }
        IGenericRepository<Writer> WriterRepository { get; }

        void Dispose();
        void Save();
    }