0

I'm currently in the process of working through a Unit Testing in C# course - specifically for NUnit (I'm using NUnit 3.8.1 with NUnit3TestAdapter 3.1.13).

My issue in question, is that when I'm testing for an exception to be thrown, my debugger stops & throws an exception on the method that I'm testing. I'm not sure if I'm mistaken with the behaviour, but I can't see what I've done wrong.

Firstly - I've read up on the documentation & can't see a problem.

I've also looked through various other posts on stack overflow that just don't seem to quite point at the answer, as none of these solutions have worked for me.

Is there some Assert method which can be used for unit test on whether a method throws an exception?

C# Unit Testing - Test method throwing NullReferenceException

Why, when I am testing that a method throws an exception and the method throw an exception, does the test stop?

The class I'm testing is as follows (and was provided from the instructor on the course - they have demonstrated that this works).


using System;

namespace TestNinja.Fundamentals
{
    public class ErrorLogger
    {
        public string LastError { get; set; }

        public event EventHandler<Guid> ErrorLogged; 

        public void Log(string error)
        {
            if (String.IsNullOrWhiteSpace(error))
                throw new ArgumentNullException();

            LastError = error; 

            // Write the log to a storage
            // ...

            ErrorLogged?.Invoke(this, Guid.NewGuid());
        }
    }
}

And finally, my test methods -

using NUnit.Framework;
using TestNinja.Fundamentals;

namespace TestNinja.UnitTests
{
    [TestFixture]
    public class ErrorLoggerTests
    {
        //private ErrorLogger _errorLogger;

        [SetUp]
        public void SetUp()
        {
            //_errorLogger = new ErrorLogger();
        }

        [Test]
        public void Log_WhenCalled_SetTheLastErrorProperty()
        {
            var logger = new ErrorLogger();

            logger.Log("a");

            Assert.That(logger.LastError, Is.EqualTo("a"));
        }

        [Test]
        [TestCase(null)]
        [TestCase("")]
        [TestCase(" ")]
        public void Log_InvalidError_ThrowArgumentNullException(string error)
        {
            var logger = new ErrorLogger();
            Assert.That(() => logger.Log(error), Throws.ArgumentNullException);
        }
    }
}

The test I'm talking about is Log_InvalidError_ThrowArgumentNullException. On each test case the code breaks on the line throw new ArgumentNullException(); in ErrorLogger. I have to close the box & continue running the code.

When I run this test individually, the test runs to completion, however when I run all, that's where the exception is thrown & my testing is stopped.

Originally, my unit testing had the SetUp method uncommented & I was using that private field, however I swapped over to a fresh instance to make sure there weren't any issues with lingering objects.

Webbarr
  • 168
  • 1
  • 10
  • 2
    You could try `Assert.Throws(() => logger.Log(error));`, but I don't see anything wrong with what you're doing. Are you running the tests as debug? Are you sure you don't have a break-point hanging out on that line? What exception are you getting when the tests fail? – Jonathon Chase Jul 18 '19 at 20:13
  • Yes, I'm running them in debug. The keyboard shortcut for run all tests is set to `Ctrl R, A` but run all tests in debug is `Ctrl R, Ctrl A`. I just double checked them. I've used the run all tests button to confirm. You've solved the problem of my own stupidity. – Webbarr Jul 18 '19 at 20:20
  • Those are the best solutions: they require 0 code changes. – Jonathon Chase Jul 18 '19 at 20:21
  • @JonathonChase Do you think this question is worth leaving up? I'm not sure if anyone else is likely to be as daft as I've been! Or should I just let the mods decide? – Webbarr Jul 18 '19 at 20:23
  • Nobody is voting to close, and at least one person has made the mistake. These things happen. – Jonathon Chase Jul 18 '19 at 20:25

0 Answers0