我刚刚浏览了关于模拟和依赖注入主题的问题和博客。得出一个结论,我只需要模拟客户端使用的接口。我期待在这里测试一个简单的用例而不知道。
合同
public Interface IApplicationService
{
bool DeleteApplication(int id);
ApplicationDto AddApplication(ApplicationDto application);
IEnumerable<ApplicationDto> GetApplications();
}
实施(我要模拟)
public Class ApplicationService:IApplicationService
{
private EntityFrameworkRepo repo;
public ApplicationService()
{
repo = new EntityFrameworkRepo();
}
public ApplicationDto Add(ApplicationDto dto)
{
//add to dbcontext and commit
}
}
模拟代码
[Test(Description = "Test If can successfully add application")]
public void CanAddApplication()
{
//create a mock application service
var applicationService = new Mock<IApplicationService>();
//create a mock Application Service to be used by business logic
var applicationDto = new Mock<ApplicationDto>();
//How do i set this up
applicationService.Setup(x => x.GetApplications()).Returns(IEnumerable<applicationDto.Object>);
}
而且我确信我需要测试业务逻辑而不是嘲笑它。那么我到底要做什么来测试我的ApplicationService
,然后将实体框架排除在外。
顺便说一句ApplicationService
,它使用带有 NInject 的构造函数注入。那么用它来模拟这个NInject.MockingKernel
会设置依赖链吗?