Question

I am trying to get a grasp on what to test and what not to test.

Given this very simple utility class:

public static class Enforce
{
    public static void ArgumentNotNull<T>(T argument, string name)
    {
        if (name == null)
            throw new ArgumentNullException("name");

        if (argument == null)
            throw new ArgumentNullException(name);
    }
}

Would you say that the following tests are enough? Or do I need to also test the reverse condition that valid arguments do in fact NOT throw?

[Fact]
    public void ArgumentNotNull_ShouldThrow_WhenNameIsNull()
    {
        string arg = "arg";
        Action a = () => Enforce.ArgumentNotNull(arg, null);

        a.ShouldThrow<ArgumentNullException>();
    }

    [Fact]
    public void ArgumentNotNull_ShouldThrow_WhenArgumentIsNull()
    {
        string arg = null;
        Action a = () => Enforce.ArgumentNotNull(arg, "arg");

        a.ShouldThrow<ArgumentNullException>();
    }

Do you need to test reverse conditions in general? Or is it safe to assume in this case?

Just a note, I am using xUnit and FluentAssertions.

Was it helpful?

Solution

The point of unit testing is to test the code that you write. Given that ArgumentNullException is part of an API that you use, testing whether that behaves according to your expectation is testing the API and not your code and would only muddy waters.

The unit test you have tests for all behaviors of the method you wrote for the code you wrote and is hence sufficient.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top