Question

I have to test this class:

public class MemberHelper
{
    private readonly MemberInfo _info;

    public MemberHelper(MemberInfo info)
    {
        _info = info;
    }
    ...
}

so in a unit test class I decided to use Unity to resolve two versions of this class based upon the type of MemberInfo I pass, in this way:

[SetUp]
    public void Setup()
    {
        _ioc = new UnityContainer();
        _dummy = new NullableDummy();

        var field = _dummy.GetType().GetField("nullableField") as FieldInfo;
        var property = _dummy.GetType().GetProperty("NullableProperty") as PropertyInfo;

        _ioc.RegisterType<MemberHelper>("fieldHelper", new InjectionConstructor(field));
        _ioc.RegisterType<MemberHelper>("propertyHelper", new InjectionConstructor(property));

        _line = "2014-03-0300012300";
    }

    [Test]
    public void ShouldDeserializeInNullableField()
    {
        _member = _ioc.Resolve<MemberHelper>("fieldhelper");
        _member.SetValue(_dummy, _line.Substring(0,10));

        Assert.IsTrue(_dummy.nullableField.HasValue);
    }

When I execute the test, it returns an InvalidOperationException: MemberInfo doesn't have a callable constructor. Why does it try to call MemberInfo constructor when I have just passed an InjectionConstructor with a well ready parameter?

I also tried to call the Resolve method passing a ParameterOverride with the same parameter passed to InjectionConstructor and it has worked!

Can someone explain me because all of this happens? Thank you in advance.

Was it helpful?

Solution

Edit: Change of answer

Replace

_member = _ioc.Resolve<MemberHelper>("fieldhelper");

with

_member = _ioc.Resolve<MemberHelper>("fieldHelper");

The h in fieldhelper is capitalised in one instance and not the other

edit: Here is the full working code http://pastebin.com/kuxDgxPL

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top