1

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?

1
  • 1
    mock the required dependencies of the controller for the test and assert the desired behavior when the test is exercised. Commented May 21, 2018 at 15:41

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Mkosi, I think I understand now, so I'm just testing the interface. So I do not need to get data from the database. I think I understand, I'm just now with an error in the method var actionResult = controller.Index () as ViewResult; but i will tifx it! Thanks so much
@PauloJardim You use the mocked interface to test the controller action in isolation without any knock on effects of the actual implementation concerns.
Anotherthing, do you know any reason for this error? System.IO.FileNotFoundException: Could not load file or assembly 'System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. this throw when I return the result of var actionResult = controller.Index() as ViewResult;
Check the the test project has the necessary libraries referenced. It is probably looking for the ViewResult class. clean and rebuild project to see if it fixes the problem.
Worked, i had to use the same System.web.mvc dll from the web project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.