MockingKernel 将多绑定命名为构造函数参数
MockingKernel Named Multi-Bindings as constructor argument
假设我有一个具有两个实现的接口
public interface IWeapon
{
string Hit(string target);
}
public class Sword : IWeapon
{
public string Hit(string target)
{
return "Slice " + target + " in half";
}
}
public class Dagger : IWeapon
{
public string Hit(string target)
{
return "Stab " + target + " to death";
}
}
我有一个接受 Sword
和 Dagger
的对象。我使用 Named
多重绑定,如 docs.
中所述
public class Samurai: ISamurai
{
private readonly IWeapon sword;
private readonly IWeapon dagger;
public Samurai(
[Named(nameof(Sword))] IWeapon sword,
[Named(nameof(Dagger))] IWeapon dagger)
{
this.sword = sword;
this.dagger = dagger;
}
}
当我在 运行 的情况下将它与正常绑定一起使用时,这工作正常:
DependencyInjector.Kernel.Bind<IWeapon>().To<Sword>().Named(nameof(Sword));
DependencyInjector.Kernel.Bind<IWeapon>().To<Dagger>().Named(nameof(Dagger));
当我使用模拟内核时这不起作用:
Mock<IWeapon> mockSword = new Mock<IWeapon>();
Mock<IWeapon> mockDagger = new Mock<IWeapon>();
MockingKernel.Rebind<IWeapon>().ToConstant(mockSword.Object).Named(nameof(Sword));
MockingKernel.Rebind<IWeapon>().ToConstant(mockDagger.Object).Named(nameof(Dagger));
MockingKernel.Get<Samurai>();
我收到以下类型的错误:
Message: OneTimeSetUp: Ninject.ActivationException : Error activating
IWeapon
No matching bindings are available, and the type is not self-bindable.
Activation path:
2) Injection of dependency IWeapon into parameter
sword of constructor of type ISamurai
1) Request for ISamurai
Rebind<IWeapon>()
方法的问题在于它会删除所有基于 IWeapon
的绑定,即使您稍后指定 Named
语法也是如此。这意味着第二个重新绑定调用消除了第一个。所以试试这个:
MockingKernel.Rebind<IWeapon>().ToConstant(mockSword.Object).Named(nameof(Sword));
MockingKernel.Bind<IWeapon>().ToConstant(mockDagger.Object).Named(nameof(Dagger));
假设我有一个具有两个实现的接口
public interface IWeapon
{
string Hit(string target);
}
public class Sword : IWeapon
{
public string Hit(string target)
{
return "Slice " + target + " in half";
}
}
public class Dagger : IWeapon
{
public string Hit(string target)
{
return "Stab " + target + " to death";
}
}
我有一个接受 Sword
和 Dagger
的对象。我使用 Named
多重绑定,如 docs.
public class Samurai: ISamurai
{
private readonly IWeapon sword;
private readonly IWeapon dagger;
public Samurai(
[Named(nameof(Sword))] IWeapon sword,
[Named(nameof(Dagger))] IWeapon dagger)
{
this.sword = sword;
this.dagger = dagger;
}
}
当我在 运行 的情况下将它与正常绑定一起使用时,这工作正常:
DependencyInjector.Kernel.Bind<IWeapon>().To<Sword>().Named(nameof(Sword));
DependencyInjector.Kernel.Bind<IWeapon>().To<Dagger>().Named(nameof(Dagger));
当我使用模拟内核时这不起作用:
Mock<IWeapon> mockSword = new Mock<IWeapon>();
Mock<IWeapon> mockDagger = new Mock<IWeapon>();
MockingKernel.Rebind<IWeapon>().ToConstant(mockSword.Object).Named(nameof(Sword));
MockingKernel.Rebind<IWeapon>().ToConstant(mockDagger.Object).Named(nameof(Dagger));
MockingKernel.Get<Samurai>();
我收到以下类型的错误:
Message: OneTimeSetUp: Ninject.ActivationException : Error activating
IWeapon
No matching bindings are available, and the type is not self-bindable.
Activation path:
2) Injection of dependency IWeapon into parameter
sword of constructor of type ISamurai
1) Request for ISamurai
Rebind<IWeapon>()
方法的问题在于它会删除所有基于 IWeapon
的绑定,即使您稍后指定 Named
语法也是如此。这意味着第二个重新绑定调用消除了第一个。所以试试这个:
MockingKernel.Rebind<IWeapon>().ToConstant(mockSword.Object).Named(nameof(Sword));
MockingKernel.Bind<IWeapon>().ToConstant(mockDagger.Object).Named(nameof(Dagger));