Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem with automocking named interfaces #17

Closed
RoSchvisual4 opened this issue Jun 3, 2014 · 2 comments
Closed

problem with automocking named interfaces #17

RoSchvisual4 opened this issue Jun 3, 2014 · 2 comments
Assignees

Comments

@RoSchvisual4
Copy link

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
Copy link
Member

What do you mean regarding "named attribute"?

@JustasSasnauskas
Copy link

JustasSasnauskas commented Oct 26, 2017

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>(); 
    }
}

@scott-xu scott-xu self-assigned this Oct 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants