J
J
Jay Marlow2015-10-18 20:16:24
C++ / C#
Jay Marlow, 2015-10-18 20:16:24

How to test a list in C# NUnit?

I never really understood this topic, but then it took me to test the list in the class.
There are several variants of the tests themselves, but they do not use the class as such, but test directly in themselves.
For example:

[Test]
        public void test()
        {
            var expected = new List<int> { 1, 2, 3 };
            var actual = new List<int> { 1, 2, 3 };

            CollectionAssert.AreEqual(expected, actual);  //1
            Assert.AreEqual(expected, actual);           //2
            Assert.That(actual, Is.EqualTo(expected));  //3
        }

How to tie this test to a list in the class, for example, this:
public class listtest
{
    private List<string> tlist = new List<string>();
    //write
    public void Write(string s)
    {
        tlist.Add(s);
    }
    //index elements
    public string ReadElement(int i)
    {
        return tlist.ElementAt(i);
    }
}

I came here already in desperation - I re-read several topics on stackoverflow (and there are only three or four of them on this topic) and other list portals, but everywhere there is always something completely different from what I need.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MrDywar Pichugin, 2015-10-18
@kolumbou

Your listtest class encapsulates a List in itself, there is no way you can get full access to it without providing it in the class.
Method names are strange, it is better to replace Write with Add.
The meaning is not clear, you have an adapter or a proxy. Determine the exact task of the class, perhaps there are already ready-made implementations.
According to the test, make a method that will return a copy of the internal list, let the copies be compared.
Read Rules for Using Arrays , Choosing Between Properties and Methods .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question