Which is a good approach to test Ninject bindings?

后端 未结 3 984
灰色年华
灰色年华 2021-01-01 13:13

We use ninject in all our projects, and as you will know, sometimes it becomes hard to test if the kernel would be able to resolve every type at execution time, because some

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-01 13:42

    So, what I'm asking here is how can I know that my kernel, after loading all modules and bindings, will be able to resolve every type ? Do you do any kind of Unit Test?

    I test this by looping over each of the bindings in my module(s) and checking that the Kernel can return something for them:

    [Test]
    public void AllModuleBindingsTest()
    {
        var kernel = new StandardKernel(new MyNinjectModule())
        foreach (var binding in new MyNinjectModule().Bindings)
        {
            var result = kernel.Get(binding.Service);
            Assert.NotNull(result, $"Could not get {binding.Service}");
        }
    }
    

提交回复
热议问题