简体   繁体   English

Intellitest / Pex正在尝试为对象中的私有只读字典字段创建IEqualityComparers

[英]Intellitest/Pex is attempting to create IEqualityComparers for private readonly Dictionary field in object

I created an empty class library project containing only this class. 我创建了一个仅包含此类的空类库项目。

public class DictionaryDemo
{
    private readonly Dictionary<string, int> dictionary = new Dictionary<string, int>();

    public void Add(string key, int value)
    {
        this.dictionary.Add(key, value);
    }
}

From this class I generated a test project with Intellitest. 从这个课程开始,我使用Intellitest生成了一个测试项目。 Apart from making a few formatting edits to tidy for posting here, I made no edits to this test project. 除了进行一些格式编辑以便在此处发布以进行整理之外,我没有对此测试项目进行任何编辑。 This contains only the following Intellitest test. 这仅包含以下Intellitest测试。

[PexClass(typeof(DictionaryDemo)), TestClass]
public partial class DictionaryDemoTest
{
    [PexMethod]
    public void AddTest([PexAssumeUnderTest] DictionaryDemo target, string key, int value)
    {
        target.Add(key, value);
    }
}

When I run the Intellitest method I get the following results. 当我运行Intellitest方法时,我得到以下结果。

在此输入图像描述在此输入图像描述在此输入图像描述

The actual generated tests themselves seem reasonable ish. 实际生成的测试本身似乎是合理的。 I'm not sure if the low coverage indicates that more tests should be generated to cover the complexities of the dictionary, or if that stems from the issues below. 我不确定低覆盖率是否表明应该生成更多测试以涵盖字典的复杂性,或者是否源于下面的问题。

My concern stems from the warnings. 我的担忧源于警告。 I do not understand why Intellitest would be attempting to create instances of these types. 我不明白为什么Intellitest会尝试创建这些类型的实例。 I initially assumed Intellitest was trying to set the dictionary field and using these instances to instantiate a new Dictionary . 我最初假设Intellitest试图设置dictionary字段并使用这些实例来实例化一个新的Dictionary This was undesired behaviour, so I added the [PexExplorableFromConstructor(typeof(DictionaryDemo))] attribute which should (I believe) prevent any direct setting of private fields, but apart from the warning about guessing how to create DictionaryDemo disappearing, the results were identical. 这是不受欢迎的行为,所以我添加了[PexExplorableFromConstructor(typeof(DictionaryDemo))]属性,它应该(我相信)阻止任何直接设置私有字段,但除了关于猜测如何创建DictionaryDemo消失的警告,结果是相同的。

It's worth noting that many of the types that it's attempting to create here are not even accessible. 值得注意的是,它试图在这里创建的许多类型甚至都无法访问。

So the question is basically, what's happening here? 所以问题基本上是,这里发生了什么? Is this intended behaviour for Intellitest? 这是Intellitest的预期行为吗? If this is Intellitest trying to set the dictionary field, that is unwanted behaviour. 如果这是Intellitest尝试设置dictionary字段,那就是不需要的行为。 How do I stop it, and why doesn't the [PexExplorableFromConstructor] stop it? 我如何阻止它,为什么[PexExplorableFromConstructor]不会阻止它? If that's not what's happening, why is it trying to use all of these types? 如果那不是正在发生的事情,为什么要尝试使用所有这些类型?


Additional stuff I've already tried 我已经尝试过的其他东西

I've played about with a bunch of the PexCreatable... and PexExplorable... attributes without success, including [PexCreatableByConstructor(typeof(DictionaryDemo), MaySetRemainingFieldsByReflection = false)] which seems like it should explicitly disallow the setting of the field. 我玩过一堆PexCreatable ......和PexExplorable ...属性没有成功,包括[PexCreatableByConstructor(typeof(DictionaryDemo), MaySetRemainingFieldsByReflection = false)] ,这似乎应该明确禁止该字段的设置。

I've tried creating a factory for DictionaryDemo with the same result. 我已经尝试为DictionaryDemo创建一个具有相同结果的工厂。

I've tried adding a [PexExplorableFromConstructor(typeof(Dictionary<string, int>))] but this has also had no effect. 我已经尝试添加[PexExplorableFromConstructor(typeof(Dictionary<string, int>))]但这也没有效果。

This behaviour appears to be identical between VS 2015 and VS 2017 RC. VS 2015和VS 2017 RC之间的行为似乎相同。

I ran across this problem while exploring using this to test a custom Dictionary-type class that was in our code with no unit tests. 我在探索使用它来测试我们的代码中没有单元测试的自定义Dictionary类型时遇到了这个问题。

I was able to solve the warning by creating a class that implemented the interface and telling IntelliTest to use that. 我能够通过创建一个实现接口的类并告诉IntelliTest使用它来解决警告。

The class was pretty simple: 这堂课非常简单:

public class DefaultEqualityComparer<T> : IEqualityComparer<T>
{
  readonly EqualityComparer<T> _comparer = System.Collections.Generic.EqualityComparer<T>.Default;

  public bool Equals(T x, T y) => _comparer.Equals(x, y);

  public int GetHashCode(T obj) => _comparer.GetHashCode(obj);
}

Inside the PexAssemblyInfo.cs file, I added the following attribute: 在PexAssemblyInfo.cs文件中,我添加了以下属性:

[assembly: PexUseType(typeof(DefaultEqualityComparer<int>))]

With those changes, I no longer received that warning. 随着这些变化,我不再收到这个警告。 I know this is an older question, but I'm hoping of someone else encounters this, it will help them. 我知道这是一个较老的问题,但我希望别人遇到这个问题,这会对他们有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM