Problem FakeItEasy Ninject WebServer
See original GitHub issueon a unit test [TestClass] i try to use Ninject and module fakeiteasy but the fakeiteasy dont respect singleton or current object, always create new (different hashcode) on a HttpServer local unit test calling internal with HttpClient, and creating fake object on [TestMethod] things like MustHaveHappend Always fail but another object created and its called.
[TestInitialize]
kernel <-- fakeiteasy (standarkernel load fakeiteasymodule)
kernel.bind<isomething>.ToMock().InSingletonScope();
config.DependencyResolver = new NinjectResolver(kernel);
HttpServer _server = new HttpServer(config);
//using Effort too but out of scope here
[TestMethod]
client = new HttpClient(_server);
request = new HttpRequestMessage(); //uri, headers, method, etc
obj = kernel.Get<isomething>();
A.Call.obj ... DoesNothing(); //try with and without
using(response =client.SendAsync(request).Result)
{
A.Call ... MustHaveHappend <-- fail
}
if i debug and check HashCode when code execute de Controller on the webapi (project on same solution) [run on the same process, same thread i think (checked on Thread window] and on unittest method check hashcode and they are different objects.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Issue Unit Testing EF & ASP.NET Identity ...
I've no information about the database stuff, but think I have an idea for the A.CallTo . FakeItEasy is unhappy at being asked...
Read more >Ninject - Open source dependency injector for .NET
Ninject helps you use the technique of dependency injection to break your applications into loosely-coupled, highly-cohesive components, and then glue them ...
Read more >Catching Up On Coding - TDD Part 3
FakeItEasy is more natural language and less ambiguous. My reaction to seeing them side-by-side is that I like FIE's syntax more. I like...
Read more >Location of tooling projects [ was: Move to fsprojects? ] #69
Question : Should we create new organization ( fstools for example) for "infrastructure" projects (FAKE, Paket, F# Formatting, FsCheck, FsUnit & ...
Read more >Mastering Ninject For Dependency Injection Pdf (2022)
to server to mobile—and now you, too, need to learn the ... Solve all your Spring 5 problems using complete and real-world code...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I can confirm it has nothing to do with FakeItEasy. In fact, it doesn’t even have anything to do with Ninject.MockingKernel: the same behavior can be observed without mocks, like this:
I’m pretty sure it wouldn’t do what @elkami had in mind, at least not on its own.
In ASP.NET (and probably many other backend frameworks), there’s a notion of “request scope”. When a service is registered with request scope, the IoC container will return the same instance during the processing of a given request, but a different one for each request. That’s what the
IDependencyResolver.BeginScope
method means: begin a request scope in which a fresh instance of each “request-scoped” service will be created.However, when a service is registered as a singleton, it should always return the same instance for all requests, regardless of scopes.
As far as I can tell, Ninject doesn’t seem to support that. Even without the DependencyResolver infrastructure, just using
IKernel.BeginBlock()
, Ninject doesn’t reuse instances of singletons but always create a new instance when in a new block. So… Maybe I’m missing something, but Ninject doesn’t seem to properly handle the singleton lifetime, which is the simplest of all. Or, more likely maybeBeginBlock
just isn’t supposed to be used that way, and has nothing to do with “scopes” in the ASP.NET sense of the word.Anyway, there is another package, Ninject.Web.Common, which adds an
InRequestScope
extension method for when you need a “request scoped” service, and doesn’t seem to rely onBeginBlock
. So if you use that, maybe yourIDependencyResolver
implementation could just return the kernel directly instead of creating a new block, as @blairconrad suggested. See this page for more details.Ah. I made a guess, and it paid off a little. In
NinjectResolver
, I seeI thought that maybe making the new NinjectScope might interfere, and indeed if you remove the
.BeginBlock
, your original test passes - the sameISomething
is used in the test and by the controller. The test outputsNow, I definitely don’t understand either Ninject or ASP.NET MVC, so I’m not sure how good an idea making this change is. maybe it’s a terrible idea. I don’t know. But so far I don’t see any evidence that FakeItEasy is doing anything wrong.
I think best to follow up at https://github.com/ninject/Ninject.MockingKernel/issues/38, which I’ve just now noticed you created as well. That would’ve been good information to have earlier.