Search code examples
c#unit-testingunity-container

Unity, Moq, and NUnit - Controller Constructor with Parameters Not Recognized in Tests


I am very new to unit testing, so I apologize if this is not a very good question.

I have a main web project, and an accompanying NUnit tests library. I am using Unity to inject interfaces into my controller within the main project. For example:

    public class EquipmentController : Controller
{
    private readonly ILocationRepository locationContext = null;
    private readonly IRepository<EquipmentCategory> categoryContext = null;
    private readonly IEquipmentRepository equipmentContext = null;
    private readonly IRecordRepository recordContext = null;

    public EquipmentController(ILocationRepository locationRepo, IRepository<EquipmentCategory> categoryRepo, IEquipmentRepository equipmentRepo, IRecordRepository recordRepo)
    {
        this.locationContext = locationRepo;
        this.categoryContext = categoryRepo;
        this.equipmentContext = equipmentRepo;
        this.recordContext = recordRepo;
    }

The web application itself actually works as expected. However, I am encountering issues while trying to write test cases. For example, in test library, I have the following:

using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Unity.Mvc;
using Microsoft.Practices.Unity.ObjectBuilder;
using Microsoft.Practices.Unity.StaticFactory;
using Moq;
using ITEquipmentDatabase;
using ITEquipmentDatabase.Models;
using ITEquipmentDatabase.Controllers;

namespace ITEquipmentDatabase.Tests
{
    [TestFixture]
    public class EquipmentController
    {
        [Test]
        public void TestMethod()
        {
            var equipRepo = new Mock<IEquipmentRepository>();
            var categoryRepo = new Mock<IRepository<EquipmentCategory>>();
            var locationRepo = new Mock<ILocationRepository>();
            var recordRepo = new Mock<IRecordRepository>();

            EquipmentController controller = new EquipmentController(locationRepo.Object, categoryRepo.Object, equipRepo.Object, recordRepo.Object);



        }
    }
}

However, I am receiving the following error:

Error   1   'ITEquipmentDatabase.Tests.EquipmentController' does not contain a constructor that takes 4 arguments   C:\Users\Khandokar\Documents\Visual Studio 2013\Projects\IT Equipment Log\ITEquipmentDatabase.Tests\EquipmentController.cs  27  46  ITEquipmentDatabase.Tests

I have Unity referenced in my tests project and even added Bootstrapper.cs (not sure if it was necessary, but I was trying to resolve the above issue). I am quite sure I am doing something very wrong, but I am just starting to venture into unit testing and am having a bit of a difficult time.

Thanks for any advice.


Solution

  • Your test class is named EquipmentController and so is your class under test.

    Rename your test class to EquipmentControllerTests or some such.

    (The error message was the clue; note that it refers to Tests.EquipmentController).