Weird “number of generic arguments doesn't equal arity” error in unit test after program compiled and worked

蹲街弑〆低调 提交于 2019-12-12 04:49:14

问题


I created a generic class, as shown below, which worked perfectly when I coded it and tested the program in Visual Studio. However, in automated build, a unit test ran which can't deal with the assembly, although no compile or runtime error was visible previously:

Error 18 Error occurred during processing of assembly '....dll': The number of generic arguments provided doesn't equal the arity of the generic type definition. Parameter name: instantiation ...Test

The unit test has no usage of the generic class.

I commented out all code, constructor, field from the class, then auto-implemented the interfaces with Visual Studio, generating stubs with "throw new NotImplementedException()".

public class RequiredPropertiesProfile<TPropertyGroup, TProperty> 
    : IDictionary<bool, IDictionary<TPropertyGroup, ICollection<TProperty>>>
{
    // auto implementations of IDictionary, with throw new NotImplementedException();
}

It again compiled, but compiling the unit test failed again, with no visible cause. The test has "Microsoft.VisualStudio.QualityTools.UnitTestFramework" referenced.

The class is mainly to define property configurations, required to be set or not set (bool). It is supposed to be generic, to have either strings (user friendly, including helper classes), data objects or database identifiers (int type), with additional methods to translate one into the other.

I found several articles about this error, but none fit my case - especially compiling and running without error first, then stumbling across a stupid unit test, which is not even using the generic class.


回答1:


While this is not an answer what the cause of the problem might be, it helped to create a base class with a fully generic interface, then put my implementation with 1st level type bool on top:

public class RequiredPropertiesGenericBase<TState, TPropertyGroup, TProperty> 
    : IDictionary<TState, IDictionary<TPropertyGroup, ICollection<TProperty>>>
{
    private _innerDict = new Dictionary<TState, IDictionary<TPropertyGroup, ICollection<TProperty>>>();

    // _innerDict wrapping methods;
}


public class RequiredPropertiesProfile<TPropertyGroup, TProperty> 
    : RequiredPropertiesGenericBase<bool, TPropertyGroup, TProperty>
{
    public RequiredPropertiesProfile(IEnumerable<PropSetting> settings)
    {
        foreach (var set in settings)
        {
            this.AddPropertySetting(set.MustBeSet, set.PropertyGroup, set.Property);
        }
        // ...
    }

    // implementation
}

I still don't know what caused this, and it's still very strange that the initial code would compile and run, but stumble across an unit test not even creating instances of the class (but using it's assembly).

The problem would occur on several machines, first, on the build machine with automated unit tests (usually doing a total cleanup before build/test). Then also on a workstation, when running the unit test.

The problem still occured in the initial version, after removing constructor (leaving only the auto constructor) and all code, except the fake interface implementations, with the class being used nowhere.



来源:https://stackoverflow.com/questions/25982224/weird-number-of-generic-arguments-doesnt-equal-arity-error-in-unit-test-after

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!