J
J
Jay Marlow2015-10-19 16:29:18
C++ / C#
Jay Marlow, 2015-10-19 16:29:18

C# giving wrong expected number from list?

Good day.
There was a problem in the class code that uses the list.
I need to test it with nunit. Below is the class code and a test for it.
The problem is described at the very bottom.
Class code:

public class MyClass01
        {
            public MyClass01(string _name, int _kol)
            {
                name = _name;
                kol = _kol;
            }
            public string name;
            public int kol;
        }

          public class cAAA
        {
            List<MyClass01> my = new List<MyClass01>();
            public cAAA()
            {
                my.Add(new MyClass01("one", 1));
                my.Add(new MyClass01("two", 2));
                my.Add(new MyClass01("three", 3));
                
            }
       
            public List<MyClass01> Read(string _name)
            {
                List<MyClass01> x = new List<MyClass01>();

                foreach (var item in my)
                {
                    if (item.name == _name)
                    {
                        x.Add(item);
                    }
                }
                return x;
            }
        }

Test code:
[Test()]
        public void Test1()
        {
          var instance = new ArrayUtils.cAAA();
          List<ArrayUtils.MyClass01> actualList = instance.Read("three");

            // проверить, что элемент 3
            Assert.AreEqual(3, actualList.Count);

            int actual = actualList.Single().kol; // извлечь элемент
         
        }

What is the point: in the end, when asked, let's say "two" or "three" - it always gives a unit to the test .
It turns out that the test is true only under the condition "one".

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kovalsky, 2015-10-19
@kolumbou

You are comparing the number of elements in the list and 3. You have all the elements under one instance. What do you think is the bug?
PS And for naming it is necessary to force coding while sitting on a stool. And with a mouse on a ball without scrolling.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question