2

我需要一些帮助来使用 ninject 的上下文绑定我有这样的事情:

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";
    }
}

以及实现 Sound 和我的绑定模块的完全相同的 Dog Sound:

 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;


    }
}

和我试图运行的测试

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

                sut.Load();



            }

它不知道我应该如何在这里断言

4

1 回答 1

1

尝试这样的事情:

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

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

用枚举替换 SelectorMode 类

public enum SelectorMode
{  
    Cat, Horse, Dog   
}
于 2017-04-12T14:46:42.070 回答