ninject / Ninject.MockingKernel

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

problem with automocking named interfaces #17

Closed RoSchvisual4 closed 6 years ago

RoSchvisual4 commented 10 years ago

Hello Team,

automocking will fail if interfaces are decorated with the ninject named attribute. I don't know if this is by design or a bug. (Interestingly i haven't found anyone complaining about it except me ...) But in my opinion it would be much better if named attributes are ignored by the mocking kernel and automocked as well because the stub or mock can be configured anyway and it would be much easier if they get automocked like the rest.

I already have a working solution but i am by far no expert for ninject internals and so my solution feels a bit "unnatural". I can share it if you like though. My idea was to create new bindings (by using IMissingBindingResolver) and manipulate their metadata so an exact match with the named attribute is generated. This way automocking works as if no named attribute is used.

Best Regards and keep up the extremly good work! Robert Schadt

scott-xu commented 9 years ago

What do you mean regarding "named attribute"?

JustasSasnauskas commented 6 years ago

Hi,

I have also stumbled on issue when Constructor have marked parameter with named attribute. Ninjec version 3.2.2 Ninject.MockingKernel 3.2.2

Is this suppose to work like that? Here is modified Example:

using System;
using Ninject;
using Ninject.MockingKernel.Moq;
using NUnit.Framework;

public interface IBar
{
    string GetContent();
}
public interface IFoo
{
    void DoStuff();
}

public class MyFoo : IFoo
{
    private readonly IBar _bar;
    public MyFoo([Named("OneNameBinding")]IBar bar)
    {
        _bar = bar;
    }

    public void DoStuff()
    {
        Console.WriteLine(_bar.GetContent());
    }
}

[TestFixture]
public class MyTests
{
    private readonly MoqMockingKernel _kernel;

    public MyTests()
    {
        _kernel = new MoqMockingKernel();
        _kernel.Bind<IFoo>().To<MyFoo>();
    }

    [SetUp]
    public void SetUp()
    {
        _kernel.Reset();
    }

    [Test]
    public void Test1()
    {
         var foo = _kernel.Get<IFoo>(); 
    }
}