# Unit testing, Exercise Stack

**URL:** <https://forum.codewithmosh.com/t/unit-testing-exercise-stack/22314>\
**Category:** C#\
**Created:** [September 1, 2023, 9:36am UTC](https://forum.codewithmosh.com/t/unit-testing-exercise-stack/22314 "2023-09-01T09:36:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Davec6505](https://avatars.discourse-cdn.com/v4/letter/d/fbc32d/32.png) [@Davec6505](https://forum.codewithmosh.com/u/Davec6505)\
**Post date:** [September 1, 2023, 9:36am UTC](https://forum.codewithmosh.com/t/unit-testing-exercise-stack/22314/1 "2023-09-01T09:36:08Z")

</div>

Hi, using the example code from the Unit test exercise, I am unable to get this to throw an exception no matter what I do.

```
    [Test]
    public void Push_NotNull_ThrowException()
    {
       var stack = new Stack<string>();
        Assert.That(() => stack.Push(null), Throws.TypeOf<ArgumentNullException>());
    }

```

Is this an environment issue as I am using the same Version of NUnit as Mosh here?

Below is the result of the test.

Message:  
Expected: \<System.ArgumentNullException\>  
But was: no exception thrown

Stack Trace:  
StackTests.Push\_NotNull\_ThrowException() line 18

I will answer this now that I have figured out the error of my ways here, I did not reference Fundamentals.Stack :frowning 😳

Just incase someone else makes the same sill mistake.

---

<div class="post-metadata">

**Author:** ![gulshan212](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/gulshan212/32/6217_2.png) [@gulshan212](https://forum.codewithmosh.com/u/gulshan212)\
**Post date:** [September 1, 2023, 5:34pm UTC](https://forum.codewithmosh.com/t/unit-testing-exercise-stack/22314/2 "2023-09-01T17:34:21Z")

</div>

Well, I think there are some logical issues with your code.  
Please try this code once.

```auto
public class Stack<T>
{
    private List<T> items = new List<T>();

    public void Push(T item)
    {
        if (item == null)
        {
            throw new ArgumentNullException(nameof(item), "Item cannot be null.");
        }
        items.Add(item);
    }
}

```

I hope this code will fix your error.  
Thanks

---

<div class="post-metadata">

**Author:** ![Davec6505](https://avatars.discourse-cdn.com/v4/letter/d/fbc32d/32.png) [@Davec6505](https://forum.codewithmosh.com/u/Davec6505)\
**Post date:** [September 1, 2023, 5:52pm UTC](https://forum.codewithmosh.com/t/unit-testing-exercise-stack/22314/3 "2023-09-01T17:52:33Z")

</div>

Excellent advice thank you gulshan212

---

<div class="post-metadata">

**Author:** ![daprog](https://avatars.discourse-cdn.com/v4/letter/d/96bed5/32.png) [@daprog](https://forum.codewithmosh.com/u/daprog)\
**Post date:** [December 2, 2023, 10:04pm UTC](https://forum.codewithmosh.com/t/unit-testing-exercise-stack/22314/4 "2023-12-02T22:04:43Z")

</div>

What does the T stands for?  
Thank you!

---

<div class="post-metadata">

**Author:** ![UniqueNospaceShort](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uniquenospaceshort/32/1814_2.png) [@UniqueNospaceShort](https://forum.codewithmosh.com/u/UniqueNospaceShort)\
**Post date:** [December 3, 2023, 7:40am UTC](https://forum.codewithmosh.com/t/unit-testing-exercise-stack/22314/5 "2023-12-03T07:40:04Z")

</div>

Hello,

I am pretty new to nUnit myself but I am using it daily for the last 2 weeks at work.

Try to use [Assert.Throws](https://docs.nunit.org/articles/nunit/writing-tests/assertions/classic-assertions/Assert.Throws.html) instead.

As for the `T` part it belongs to generic programming.

Basically instead of doing the same code for different types you use a generic parameter to represent a type. You can also [add constraints](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/where-generic-type-constraint) to tell what is allowed or not to be a `T`.

Cheers.

---

<div class="post-metadata">

**Author:** ![dimanjenje](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/dimanjenje/32/331_2.png) [@dimanjenje](https://forum.codewithmosh.com/u/dimanjenje)\
**Post date:** [July 26, 2024, 9:10am UTC](https://forum.codewithmosh.com/t/unit-testing-exercise-stack/22314/6 "2024-07-26T09:10:51Z")

</div>

This is Mosh code…

 ![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/8/8477fd2bb0dafad58eab252db7ae3b2de7eb645a.jpeg)

On your test you may be using the common stack of the .net framework that is in System.Collections.Generic… you need reference the stack created in fundalmentals Folder

![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/f/f02dfefe3bb13b48ee9565421b4b1884614fd5ca.png)
