IList<Tuple<T1, T2>> has got entries in the constructor, then not?!

RumSchubser

New member
Joined
Oct 4, 2024
Messages
1
Location
Bavaria
Programming Experience
10+
Hi there,
here's my very first post...

I've got a class called InterpolatingDictionary. I won't bother you with all the glory details. Here's what bothers me:

Internally, the dictionary actually contains a List<Tuple<T1, T2>> which is then used as kind of a looktup table.

So I did write my unit tests, and wtf?! Assert.IsInstanceOfType<InterpolatingDictionary<float, float>>(idict.GetType()) results in an assertion saying that the actual instance is of type System.RuntimeType. Within the unit test there's a method that will create such a List<Tuple<float, float>> that then will be handed to the constructor of the InterpolatingDictionary. Within the constructor the items are visible. Back in the test class, count is 0.

Yeah, I guess there's a lot of information missing, but ask if you feel like wanting to help. I don't want to spam this very first post of mine with like tons of lines of code without being asked to do so.
 
Which library or namespace is this Assert is from? Is it NUnit, Microsoft Testing, xUnit, etc?

In most of the versions of IsInstanceOf() I've seen have you pass in the object, not the type of the object. So in your case it should be:
Assert.IsInstanceOfType<InterpolatingDictionary<float, float>>(idict)
without the GetType()
 
Within the unit test there's a method that will create such a List<Tuple<float, float>> that then will be handed to the constructor of the InterpolatingDictionary. Within the constructor the items are visible. Back in the test class, count is 0.
Showing us some minimal complete code for this would help dramatically in us trying to understand what issue you are having and see what you are doing.

For example the following works fine for me:
C#:
using Xunit;

namespace UnitTest
{
    class MyDictionary<T1, T2> where T1 : notnull
    {
        Dictionary<T1, T2> _inner = new Dictionary<T1, T2>();

        public int Count => _inner.Count;

        public MyDictionary(Func<(T1 key, T2 value)> createEntry, int count)
        {
            for(int i = 0; i < count; i++)
            {
                var (key, value) = createEntry();
                _inner.Add(key, value);
            }
        }
    }

    public class MyDictionary
    {
        [Fact]
        public void CanConstruct()
        {
            Random _random = new Random();
            var dictionary = new MyDictionary<int, int>(() => (_random.Next(), _random.Next()), 3);

            Assert.Equal(3, dictionary.Count);
            Assert.IsType<MyDictionary<int, int>>(dictionary);
        }
    }
}

1728060870940.png
 

Latest posts

Back
Top Bottom