Unit Testing Unit of Work and Generic Repository Pattern framework using Moq

点点圈 提交于 2019-12-04 20:13:38

Move creation of the _platformService after Arrange step. Because you call the PlatformService constructor before unitOfWork mock is setup.

_subsiteRepository = new Mock<IRepository<Subsite>>();
_unitOfWork = new Mock<IUnitOfWork<PlatformContext>>();

// Arrange
var fakeSubsites = new List<Subsite>
{
    new Subsite {IDSubsite = new Guid(), Title = "Subsite One"}
}.AsQueryable();

_unitOfWork.Setup(x => x.GetRepository<Subsite>()).Returns(_subsiteRepository.Object);
_unitOfWork.Setup(x => x.GetRepository<Subsite>().GetAll()).Returns(fakeSubsites);

// Act
_platformService = new PlatformService(_unitOfWork.Object);
var subsites = await _platformService.GetSubsites(null, null);

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