Unit Testing the Repository Pattern Without EF [closed]

那年仲夏 提交于 2021-01-20 13:40:07

问题


I have created a simple Web API that interfaces with a MySQL Database. I am old school and like using SqlConnection/SqlCommand classes to interact with the database, no ORM/no EF.

I have created a MySQL DbContext so that I can use DI to inject the MySQL specifics and interactions into each entity repository class for CRUD operations.

public interface ICompanyDbContext
{
     public List<Company> GetAll();
     public Company GetById(string companyId);
     public Company Store(Company e);
     public Company Update(Company e);
     public void DeleteById(string entityId);
     public Company GetCompanyByApiKey(string ApiKey);

}

I know that using the repository pattern, the DbContext pattern with DI that I can mock both in my unit test project. When I use Moq and mock the DbContext and Repository, I find myself much of the code in my repository class is simply offloading heavy lifting to the MySQL db context. What should my unit tests be testing? With no SQL db, what am I testing aside from methods associated with input data validation and such?

private ICompanyDbContext _companyDbContext;

        public CompaniesRepository(ICompanyDbContext dbContext)
        {
            _companyDbContext = dbContext;
        }

        public List<Company> GetAll()
        {
            List<Company> returnList = new List<Company>();
            returnList = _companyDbContext.GetAll();


            return returnList;
        }        
        
        public Company GetById(string companyId)
        {
            Company company = this._companyDbContext.GetById(companyId);
            return company;
        }

What would a CRUD (GetAll, Get, Store, Update) repo unit test class look like?

[TestClass]
    public class CompaniesRepositoryTests
    {
        private Mock<CompanyDbContext> _companyDbContext = null;
        private Mock<CompaniesRepository> _companiesRepository = null;


        [TestInitialize]
        public void Initialize()
        {
            _companyDbContext = new Mock<CompanyDbContext>();
            _companiesRepository = new Mock<CompaniesRepository>(_companyDbContext);
        }

        [TestMethod]
        public void TestGetAllCompanies()
        {
            //TODO: Add correct test logic here
            Assert.IsTrue(true);
        }

        [TestMethod]
        public void TestGetCompanyById()
        {
            //TODO: Add correct test logic here
            Assert.IsTrue(true);
        }

来源:https://stackoverflow.com/questions/65785898/unit-testing-the-repository-pattern-without-ef

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!