ninject / Ninject.MockingKernel

Extension for Ninject aiding testability in Moq, NSubstitute, and FakeItEasy
http://ninject.org/
Other
62 stars 25 forks source link

Problem FakeItEasy Ninject WebServer #38

Open elkami opened 5 years ago

elkami commented 5 years ago

on 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.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(); 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.

blairconrad commented 5 years ago

For what it's worth, I did a little analysis over at https://github.com/FakeItEasy/FakeItEasy/issues/1597#issuecomment-515272165, but the only library I understand even a little bit in all this is FakeItEasy, so I may be off-base. Highlights follow


In NinjectResolver, I see

public IDependencyScope BeginScope()
{
    return new NinjectScope(_kernel.BeginBlock());
}

I thought that maybe making the new NinjectScope might interfere, and indeed if you remove the .BeginBlock, your original test passes - the same ISomething is used in the test and by the controller.