Ninject.MockingKernel | Extension for Ninject aiding testability in Moq | Testing library
kandi X-RAY | Ninject.MockingKernel Summary
kandi X-RAY | Ninject.MockingKernel Summary
This extension provides integration between Ninject and Moq/NSubstitute/FakeItEasy, creating a "lightweight" auto-mocking container.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Ninject.MockingKernel
Ninject.MockingKernel Key Features
Ninject.MockingKernel Examples and Code Snippets
Bind().ToMock();
Bind().ToMock(typeof(IInterface1), typeof(IInterface2));
Community Discussions
Trending Discussions on Ninject.MockingKernel
QUESTION
I'm working on adding unit tests to some legacy ASP code with Moq and the Ninject.MockingKernal.
...ANSWER
Answered 2018-Jan-19 at 12:27The above approach/design is going to cause all manner of head aches to maintain/test as the controller is tightly coupled to the kernel (IoC container) which basically does not allow one to be able to easily mock/replace it for testing.
Also note that the examples linked in question all have in common the ability to explicitly inject the dependencies into their subjects under test.
The above is basically using the kernel as a service locator.
Trying to put lipstick on that code may change its appearance but does nothing about the smell.
Ideally the design should be following the explicit dependency principle.
Methods and classes should explicitly require (typically through method parameters or constructor parameters) any collaborating objects they need in order to function correctly.
QUESTION
I've configured a MockingKernel
in order to mock a dependency:
ANSWER
Answered 2017-Feb-15 at 14:47Here's a working example:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Ninject.MockingKernel
The following syntax will bind a service to the mocked object of a Mock<T>.
If you request a service that has no binding, instead of creating an implicit self-binding, the MockingKernel will create an instance of Mock<T> and return the mocked object associated with it.
A Reset() method is available, which clears the Ninject cache of any activated instances, regardless of whether they remain in scope. You can call this method after each test to ensure that instances are reactivated, without having to dispose and re-initialize the kernel each time.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page