4

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. 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.

[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.

enter image description here enter image description here enter image description here

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. I initially assumed Intellitest was trying to set the dictionary field and using these instances to instantiate a new 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.

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? If this is Intellitest trying to set the dictionary field, that is unwanted behaviour. How do I stop it, and why doesn't the [PexExplorableFromConstructor] stop it? 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.

I've tried creating a factory for DictionaryDemo with the same result.

I've tried adding a [PexExplorableFromConstructor(typeof(Dictionary<string, int>))] but this has also had no effect.

This behaviour appears to be identical between VS 2015 and VS 2017 RC.

Sam
  • 410
  • 2
  • 10

1 Answers1

0

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.

I was able to solve the warning by creating a class that implemented the interface and telling IntelliTest to use that.

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:

[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.

Eric Olsson
  • 4,805
  • 32
  • 35