Which is a good approach to test Ninject bindings?

后端 未结 3 1083
灰色年华
灰色年华 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:40

    Based on Owen's answer but that was not working for me as is. what I had to do was pass the new instance of my dependencies into a new kernel instance and at that point the Bindings property was populated.

        [TestMethod]
        public void TestBindings
        {
            var dependencies = new MyDependencies();
            var kernel = new StandardKernel(dependencies);
    
            foreach (var binding in dependencies.Bindings)
            {
                kernel.Get(binding.Service);
            }
        }
    

    Also I chose to use kernal.Get so that if the service was not able to be resolved then the test would fail because of the error thrown as well as show the missing bindings in the message

提交回复
热议问题