奇怪的“通用论证的数量不等于对立".程序编译并运行后,单元测试中出现错误 [英] Weird "number of generic arguments doesn't equal arity" error in unit test after program compiled and worked

查看:106
本文介绍了奇怪的“通用论证的数量不等于对立".程序编译并运行后,单元测试中出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个通用类,如下所示,当我对其进行编码并在Visual Studio中测试该程序时,该类可以完美地工作.但是,在自动化构建中,尽管以前没有看到编译或运行时错误,但是运行了无法处理程序集的单元测试:

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:

错误18处理程序集'.... dll'时发生错误:提供的泛型参数数量不等于泛型类型定义的含义. 参数名称:实例化...测试

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.

我从类中注释掉了所有代码,构造函数,字段,然后使用Visual Studio自动实现接口,并使用"throw new NotImplementedException()"生成存根.

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

它再次编译,但是编译单元测试再次失败,没有明显的原因.该测试引用了"Microsoft.VisualStudio.QualityTools.UnitTestFramework".

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

该类主要用于定义需要设置或不设置(布尔)的属性配置.它应该是通用的,具有字符串(用户友好,包括帮助程序类),数据对象或数据库标识符(int类型),以及将它们相互转换的其他方法.

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.

推荐答案

虽然这不是问题根源的答案,但它有助于创建具有完全通用接口的基类,然后将实现与第一级输入 bool 在顶部:

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.

这篇关于奇怪的“通用论证的数量不等于对立".程序编译并运行后,单元测试中出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆