简体   繁体   English

Asp.Net MVC 5测试IoC +依赖注入

[英]Asp.Net MVC 5 Testing IoC + Dependency Injection

I'm trying to test my project. 我正在尝试测试我的项目。 I have never used tests before and I am starting to learn I would like a help, in the simplest case I want test this public ActionResult Index() but I don't know how to Inject those dependencies. 我以前从未使用过测试,并且我开始学习我想要的帮助,在最简单的情况下,我想测试此public ActionResult Index()但是我不知道如何注入那些依赖项。

Controller: 控制器:

Controller: 控制器:

public class WorkPlacesController : Controller
{
    private readonly IWorkPlaceService workPlaceService;

    public WorkPlacesController(IWorkPlaceService workPlaceService)
    {
        this.workPlaceService = workPlaceService;
    }
    // GET: WorkPlaces
    public ActionResult Index()
    {
        var workPlaces = workPlaceService.GetWorkPlaces(includedRelated:  
        true);

        return View(workPlaces);
    }
}

Here is my Service 这是我的服务

Service 服务

public class WorkPlaceService : IWorkPlaceService
{
    private readonly IWorkPlaceRepository workPlacesRepository;
    private readonly IUnitOfWork unitOfWork;


    public WorkPlaceService(IWorkPlaceRepository workPlacesRepository, IUnitOfWork unitOfWork)
    {
        this.workPlacesRepository = workPlacesRepository;
        this.unitOfWork = unitOfWork;
    }
}

public interface IWorkPlaceService
{
    IEnumerable<WorkPlace> GetWorkPlaces(string workPlaceDescription = null, bool includedRelated = true);
}

And my Repository 还有我的资料库

Repository 资料库

public class WorkPlaceRepository : RepositoryBase<WorkPlace>, IWorkPlaceRepository
{
    public WorkPlaceRepository(IDbFactory dbFactory)
            : base(dbFactory) { }


    public WorkPlace GetWorkPlaceByDescription(string workPlaceDescription)
    {
        var workPlace = this.DbContext.WorkPlaces.Where(c => c.Description == workPlaceDescription).FirstOrDefault();

        return workPlace;
    }
}

public interface IWorkPlaceRepository : IRepository<WorkPlace>
{
    WorkPlace GetWorkPlaceByDescription(string workPlaceDescription);


}

Factory

public class DbFactory : Disposable, IDbFactory
{
    AgendaEntities dbContext;

    public AgendaEntities Init()
    {
        return dbContext ?? (dbContext = new AgendaEntities());
    }

    protected override void DisposeCore()
    {
        if (dbContext != null)
            dbContext.Dispose();
    }
}

I tried to do something like this: 我试图做这样的事情:

public void BasicIndexTest()
{
    // Arrange
    var mockRepository = new Mock<IWorkPlaceService>();
    var controller = new WorkPlacesController(mockRepository.Object);
    // Act
    ActionResult actionResult = controller.Index() as ViewResult;           
    // Assert
    Assert.IsInstanceOfType(actionResult, typeof(List<WorkPlace>));
}

How do I inject in this controller the data needed to go in the database and bring the results? 我如何在该控制器中注入进入数据库并带来结果所需的数据?

I Want test this public ActionResult Index() but I don't know how to Inject those dependencies. 我想测试这个公共的ActionResult Index(),但是我不知道如何注入那些依赖项。

Mock the behavior of required dependencies of the controller for the test and assert the desired behavior when the test is exercised. 模拟测试所需的控制器依赖项的行为,并在执行测试时声明所需的行为。

For example, based on what you have done so far 例如,根据您到目前为止所做的事情

public void BasicIndexTest() {
    // Arrange
    var mockService = new Mock<IWorkPlaceService>();
    var workPlaces = new List<WorkPlace>() {
        new WorkPlace()
    };
    mockService
        .Setup(_ => _.GetWorkPlaces(It.IsAny<string>(), It.IsAny<bool>()))
        .Returns(workPlaces);

    var controller = new WorkPlacesController(mockService.Object);

    // Act
    var actionResult = controller.Index() as ViewResult;

    // Assert
    Assert.IsNotNull(actionResult);
    var model = actionResult.Model;
    Assert.IsNotNull(model)
    Assert.IsInstanceOfType(model, typeof(List<WorkPlace>));
    Assert.AreEqual(workPlaces, model);
}

Only the IWorkPlaceService was needed for the testing of Index action, but fake data was needed for the invocation of the GetWorkPlaces method. 测试Index动作只需要IWorkPlaceService ,而调用GetWorkPlaces方法则需要假数据。 So the mock was configured to return a list of objects when called and pass it to the view result. 因此,该模拟被配置为在调用时返回对象列表,并将其传递给视图结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM