2

I need some help with using contextual binding with ninject I Have something like this :

public interface ISound
{
    String Sound();
}

public class Cat : Animal
{
    private string category;
    private ISound sound;

    public Cat(ISound sound, int age, string name, string sex, string category)
         : base(age, name, sex)
    {
        this.sound = sound;
        this.category = category;
    }


public class CatSound : ISound
{
    public String Sound()
    {
        return "Meow";
    }
}

and exactly the same Dog Sound who implemets Sound and my bindingmodule:

 public class BindingModule:NinjectModule
{
    private readonly SelectorMode _typeofsound;

    public new StandardKernel Kernel => ServiceLocator.Kernel;

    public BindingModule(SelectorMode mode)
    {
        _typeofsound = mode;
    }


    public override  void Load()
    {
        if (_typeofsound == SelectorMode.Dog)
        {
            Kernel.Bind<ISound>().To<DogSound>();
        }
        else if(_typeofsound==SelectorMode.Cat)
        {
            Kernel.Bind<ISound>().To<CatSound>();
        }
        else
        {
            Kernel.Bind<ISound>().To<HorseSound>();
        }


    }

    public class SelectorMode
    {
        public static SelectorMode Cat;
        public static SelectorMode Horse;
        public static SelectorMode Dog;


    }
}

and the test i'm trying to run

public class WhenBindingCat:GivenABindingModule
        {
            [TestMethod]
            public void SouldBindItToCat()
            {
                // var kernel=new Ninject.StandardKernel(new  )
                var sut = new BindingModule(SelectorMode.Cat);

                sut.Load();



            }

and it don't know how i should assert here

1 Answers1

1

Try something like this:

        [TestMethod]
        public void SouldBindItToCat()
        {
            var sut = new BindingModule(SelectorMode.Cat);
            IKernel kernel = new StandardKernel(sut);

            Assert.IsTrue(kernel.Get<ISound>() is Cat);
        }

and

replace SelectorMode class by enum

public enum SelectorMode
{  
    Cat, Horse, Dog   
}
gabba
  • 2,815
  • 2
  • 27
  • 48
  • In this example this looks like the best way to do it. Another approach could be creating an interface around the kernel and mock it out. That way one can assert if the correct kernel binding has been called. – John Verbiest Apr 12 '17 at 15:01
  • the test failed, he is DogSound type , you think it might be a problem in my SelectorMode? – Alexandra Tupu Apr 12 '17 at 15:14
  • Probably you have another tests that overrides Bind – gabba Apr 12 '17 at 15:17
  • @AlexandraTupu, I have updated answer, now it must works – gabba Apr 12 '17 at 15:24
  • @AlexandraTupu What would be if you place IKernel kernel = new StandardKernel();? – gabba Apr 12 '17 at 15:47
  • failed with the Message: Test method SpecsBindings.GivenABindingModule+WhenBindingCat.SouldBindItToCat threw exception: Ninject.ActivationException: Error activating ISound No matching bindings are available, and the type is not self-bindable. Activation path: 1) Request for ISound – Alexandra Tupu Apr 12 '17 at 15:54
  • @AlexandraTupu, Oh... I got! Is SelectorMode.Cat == null? I`we replaced answer again – gabba Apr 12 '17 at 16:03