7
votes

I am trying to inject through the use of Unity two objects based on an interface into a class constructor.

I'm currently receiving the following error when unit testing:

Result Message:
Test method TestProject.TFStests.Check_Interface_CheckOut_Method threw exception: System.InvalidOperationException: The type Adp.Tools.VersionControl.TfsVersionControl.TfsVcPromotionManager does not have a constructor that takes the parameters (TfsVcQaCheckoutWorker).

The following code is my Unity class, this is used to register and resolve the TfsVCPromotionManager object:

public class UnityClass
{
    public static ITfsVcPromotionManager returnNewPromotionManager(
       VersionControlServer tfServer)
    {
        var container = new UnityContainer();

        ITfsVcQaCheckinWorker test1 = CreateUnityCheckInWorker();
        ITfsVcQaCheckoutWorker test2 = CreateUnityCheckOutWorker(tfServer);

        container.RegisterType<ITfsVcPromotionManager, TfsVcPromotionManager>(
            new InjectionConstructor(test2), new InjectionConstructor(test1));
        return container.Resolve<TfsVcPromotionManager>();
    }

    private static ITfsVcQaCheckinWorker CreateUnityCheckInWorker()
    {
        var container = new UnityContainer();

        container.RegisterType<ITfsVcQaCheckinWorker, ITfsVcQaCheckinWorker>();
        return container.Resolve<TfsVcQaCheckinWorker>();
    }

    private static ITfsVcQaCheckoutWorker CreateUnityCheckOutWorker(
        VersionControlServer passedServer)
    {
        var container = new UnityContainer();

        container.RegisterType<ITfsVcQaCheckoutWorker, TfsVcQaCheckoutWorker>(
            new InjectionConstructor(passedServer));
        return container.Resolve<TfsVcQaCheckoutWorker>();
    }
}

This is the constrcutor os the TfsVcPromotionManager class. note that it clearly takes in an instance based on the interfaces ITfsVcQaCheckoutworker and ITfsVcCheckinWorker.

 private ITfsVcQaCheckoutWorker _checkOutWorker;

    private ITfsVcQaCheckinWorker _checkInWorker;

    public TfsVcPromotionManager(ITfsVcQaCheckoutWorker checkOutWorker,
                                 ITfsVcQaCheckinWorker checkInWorker)
    {
        if (checkOutWorker == null || checkInWorker == null)
        {
            throw new NullReferenceException();
        }

        _checkOutWorker = checkOutWorker;
        _checkInWorker = checkInWorker;
    }

Can anyone give me an indication of what I'm doing wrong.

2

2 Answers

5
votes

It says it can't find a "constructor that takes the parameters (TfsVcQaCheckoutWorker)", which is indeed a true statement. There is no constructor that takes just one parameter. I'm not terrible familiar with Unity, but I bet your problem would be solved by changing this:

container.RegisterType<ITfsVcPromotionManager, TfsVcPromotionManager>(
    new InjectionConstructor(test2),
    new InjectionConstructor(test1))

To this:

container.RegisterType<ITfsVcPromotionManager, TfsVcPromotionManager>(
    new InjectionConstructor(test2, test1))

See the MSDN Reference for the InjectionConstructor's constructor.

4
votes

I believe you should register container with several params the other way. Have you tried this?

container.RegisterType<ITfsVcPromotionManager, TfsVcPromotionManager>(
    new InjectionConstructor(test2, test1)
);