tdd ioc 容器 ninject 上下文绑定
tdd ioc container ninject contextual binding
我需要一些帮助来使用 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();
}
它不知道我应该如何断言
尝试这样的事情:
[TestMethod]
public void SouldBindItToCat()
{
var sut = new BindingModule(SelectorMode.Cat);
IKernel kernel = new StandardKernel(sut);
Assert.IsTrue(kernel.Get<ISound>() is Cat);
}
和
将 SelectorMode class 替换为枚举
public enum SelectorMode
{
Cat, Horse, Dog
}
我需要一些帮助来使用 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();
}
它不知道我应该如何断言
尝试这样的事情:
[TestMethod]
public void SouldBindItToCat()
{
var sut = new BindingModule(SelectorMode.Cat);
IKernel kernel = new StandardKernel(sut);
Assert.IsTrue(kernel.Get<ISound>() is Cat);
}
和
将 SelectorMode class 替换为枚举
public enum SelectorMode
{
Cat, Horse, Dog
}