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.
Well, I think there are some logical issues with your code.
Please try this code once.
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);
}
}
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 to tell what is allowed or not to be a T.
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