0

basically, I've created a custom Assert method that asserts that an exception was thrown. It's a convenience for some unit testing I'm doing

Except it takes an Action as a parameter (obviously) but won't take a property assignment as an action.

How do I wrap the property assignment in an anonymous function?

public static class AssertException
{
    public static void DoesntThrow<T>(Action func) where T : Exception
    {
        try
        {
            func.Invoke();
        }
        catch (Exception e)
        {
            Assert.Fail("No exception was expected but exception of type " 
                + e.GetType() + " with message " + e.Message + " was thrown");
        }
    }

    public static void Throws<T>(Action func, string expectedMessage = "") where T : Exception
    {
        bool exceptionThrown = false;
        try
        {
            func.Invoke();
        }
        catch ( Exception e )
        {
            Assert.IsTrue(e.GetType() == typeof(T), "Expected exception of type " + typeof(T) 
                + " but type of " + e.GetType() + " was thrown instead");
            if (!expectedMessage.Equals(""))
            {
                Assert.AreEqual(e.Message == expectedMessage, "Expected exception with message of "
                    + expectedMessage + " but exception with message " + e.Message + " was thrown instead");
            }
            return;
        }
        Assert.Fail("Expected exception of type " + typeof(T) + " but no exception was thrown");
    }
}

And the call:

AssertException.DoesntThrow<Exception>(robot.Instructions = "RLRLMLR");

This is giving me:

Error   2   The best overloaded method match for 'RobotWarsTests.AssertException.DoesntThrow<System.Exception>(System.Action)' has some invalid arguments   C:\Users\User\Documents\Visual Studio 2012\Projects\RobotWars\RobotWarsTests\UnitTest1.cs   20  13  RobotWarsTests
M.S
  • 288
  • 4
  • 12
Mark
  • 155
  • 3
  • 16
  • Just a comment: `Throws` makes perfect sense, but are you sure that `DoesntThrow` really adds something to your unit tests? If a regular statement in a unit test throws an exception, the unit test will fail anyway, so I don't see what you gain by wrapping it in a `DoesntThrow` invocation. – Heinzi Nov 18 '14 at 08:19
  • I have constructors that throw exceptions if the initial data passed to them is invalid. This seemed the most sensible way to assert that a constructor worked properly. Then once I had the method it seemed a sensible way of doing pass tests for certain data. I'm passing data into objects and want to make sure it's not throwing out exceptions for valid data. – Mark Nov 18 '14 at 21:23
  • i suppose I just wanted to be explicit. This is a technical test for a job application so it's only a small task and doesn't have to be perfect. – Mark Nov 18 '14 at 21:24

1 Answers1

2
AssertException.DoesntThrow<Exception>(() => { robot.Instructions = "RLRLMLR"; });

This creates a lambda expression that takes no parameters () and executes the code inside the curly braces.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • thanks dude :) I actually found the solution eventually, I was using the slightly more outdated style of (new Action (delegate {})) to achieve the same effect. This will make it a little tidier though – Mark Nov 18 '14 at 06:48