Testing my own stack class



  • Im creating a very simple stack class to implement my learnings on TDD. I have created a test that will create a stack and check the length of it.

    This is my test class:

    using NUnit.Framework;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace StackTest
    {
        [TestFixture]
        class StackTests
        {
    
            [Test]
            public void CreateStack()
            {
                Stack<int> stack = new Stack<int>(5);
                Assert.AreEqual(5, stack.Size());
            }
        }
    }
    

    And here is my actual stack class:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Stack
    {
       public class Stack <T>
        {
            #region Members
            private T[] stackArray;
            private int maxLength;
            #endregion
    
            #region Properties
            public int Size { get; private set; }
            #endregion
    
            #region Constructor
            public Stack(int Length)
            {
                maxLength = Length;
                stackArray = new T[Length];
            }
            #endregion
        }
    }
    

    The problem I have is that im using the System.Collection.Generic framework here when creating the stack in my test case, but I want to use the class that I created, but visual studio can´t seem to find that.



  • First read and learn about namespaces and usings in C#. You will need that a lot in the future.

    Secondly:

    1. Don't use the same names for custom created classes. You can easily change Stack to MyStack or CustomStack and that should be easier for VS to handle it automatically.
    2. To fix this code the shorter way (but wrong way) change: using System.Collections.Generic; in StackTest class to using Stack;

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2