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.
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.
But was: no exception thrown
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.");
I hope this code will fix your error.
Excellent advice thank you gulshan212
What does the T stands for?
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 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 to tell what is allowed or not to be a