gpt4 book ai didi

c# - tdd ioc 容器 ninject 上下文绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-03 12:29:21 26 4
gpt4 key购买 nike

我需要一些帮助来使用 ninject 的上下文绑定(bind)我有这样的东西:

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 完全一样和我的绑定(bind)模块:

 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();



}

它不知道我应该如何断言

最佳答案

尝试这样的事情:

        [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
}

关于c# - tdd ioc 容器 ninject 上下文绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43372599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com