简体   繁体   中英

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.

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.

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. So the mock was configured to return a list of objects when called and pass it to the view result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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