mercredi 12 juin 2019

How do I assert an exception thrown with a switch statement (NUnit)

I have written a bunch of functional tests for a game I have been developing. However I am missing test cases that help me assert on exceptions thrown.

I am working with events as part of a StateMachine, that switches between different game states. I want to assert that creating an event with a not implemented case, invokes the default which throws an exception.

I have been reading about Assert.Throws so far with Nunit, but I cant seem to figure out how to correctly use and implement this method, when I am using events.

Below is an example of how I invoke the events inside my testcase:

        [Test]
        public void TestExceptionIsThrown() {
        Method().RegisterEvent(
            GameEventFactory<object>.CreateGameEventForAllProcessors(
                GameEventType.GameStateEvent,
                this,
                "CHANGE_STATE", // this is the message processed with Method()
                "", ""));
        Method().ProcessEventsSequentially();
        // assert on exception
        }

Inside my Method the following default case is found:

       default: throw new NotImplementedException($"Event Type: gameEvent.Message} is not supported");

However I cant seem to figure out how to use the Assert.Throw to actually assert on this, as it used with events, and not as a value, like parsing a integer into a string only function etc.

Can anyone help be figure out how to use Assert.Throws (or similar) correctly with NUnit?

Aucun commentaire:

Enregistrer un commentaire