ExpectedException Assert

Rubenex

I need to write a unit test for the next function and I saw I can use [ExpectedException]

this is the function to be tested.

public static T FailIfEnumIsNotDefined<T>(this T enumValue, string message = null)
        where T:struct
    {
        var enumType = typeof (T);

        if (!enumType.IsEnum)
        {
            throw new ArgumentOutOfRangeException(string.Format("Type {0} is not an Enum, therefore it cannot be checked if it is Defined not have defined.", enumType.FullName));
        } 
        else if (!Enum.IsDefined(enumType, enumValue))
        {
            throw new ArgumentOutOfRangeException(string.Format("{1} Value {0} is not does not have defined value in Enum of type {0}. It should not be...", enumType.FullName, message ?? ""));
        }

        return enumValue;
    }

and here would go the code to test the exceptions that are supposed to be threw

    [TestMethod] 
    [ExpectedException(ArgumentOutOfRangeException(ArgumentException), "message")]
    public void FailIfEnumIsNotDefined_Check_That_The_Value_Is_Not_Enum()
    {
        // PREPARE
        // EXECUTE
        // ASSERT
    }

I don't have idea have to make the assert for the exceptions either.

Sergey Berezovskiy

ExpectedException just asserts that exception of specified type will be thrown by test method:

[TestMethod] 
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void FailIfEnumIsNotDefined_Check_That_The_Value_Is_Not_Enum()
{
    // PREPARE
    // EXECUTE
    // NO ASSERT!!
}

If you want to assert other parameters of exception, then you should use try..catch in your test method:

[TestMethod]     
public void FailIfEnumIsNotDefined_Check_That_The_Value_Is_Not_Enum()
{
    // PREPARE

    try
    {
       // EXECUTE
       Assert.Fail()
    }
    catch(Exception exception)
    {        
        // ASSERT EXCEPTION DETAILS
    }
}

You can write your own method for asserting exception was thrown to avoid repeating same test code over and over again:

public TException AssertCatch<TException>(Action action)
    where TException : Exception
{
    try
    {
        action();
    }
    catch (TException exception)
    {
        return exception;
    }

    throw new AssertFailedException("Expected exception of type " + 
                                    typeof(TException) + " was not thrown");
}

Usage:

var exception = AssertCatch<ArgumentOutOfRangeException>(() => /* EXECUTE */);
Assert.AreEqual("foo", exception.Message);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ExpectedException Attribute Usage

From Dev

Use ExpectedException with Kotlin

From Dev

How to work with junit ExpectedException?

From Dev

phpunit testing expectedException not working

From Dev

Is possible to property to a message in ExpectedException?

From Dev

JUnit @Rule ExpectedException

From Dev

ExpectedException Attribute Usage

From Dev

Junit ExpectedException fail with explanatory message

From Dev

TestNG expectedException not finding thrown Exception

From Dev

ExpectedException in nUnit gave me an error

From Dev

ExpectedException.expectMessage((String) null) is not working

From Dev

Unable to expect multiple exceptions with JUnit's ExpectedException

From Dev

Junit ExpectedException: expect an exact Exception object

From Dev

Test with ExpectedException fails when using PoweMock with PowerMockRule

From Dev

How to continue test after JUnit ExpectedException if thrown?

From Dev

Rules that govern Checking for ExpectedException thrown in JUnit

From Dev

Alternatives to the deprecated ExpectedException.none() in JUnit 4.13

From Dev

XUnit and MSTest with ExpectedException Returning Different Results

From Dev

Combining static assert and assert?

From Dev

Assert alternative (or)

From Dev

Assert not triggering

From Dev

continue on assert

From Dev

use of assert and static assert functions

From Dev

ExpectedException attribute is not present yet in Unit Test App (Windows Universal)?

From Dev

how to make assert to wait for IdlingResource to assert

From Java

How to assert greater than using JUnit Assert?

From Dev

Assert statement Assert.assertSame is failing

From Dev

Espresso android -- Evoke assert? (NOT a view assert)

From Dev

Why Q_ASSERT instead of assert

Related Related

HotTag

Archive