Search code examples
c#unit-testingsonarqubemstest

Correctly testing that a method does nothing under certain cicrumstances


I am running into a little trouble with my unit testing code and SonarQube.

I have the following simple test:

    [TestMethod]
    public void TestFormat_Null()
    {
        _sut.Format(null);
    }

    public void Format(IPhone phoneNumber)
    {
        if (phoneNumber == null) return;
        FormatNumber(phoneNumber);
        FormatAreaCode(phoneNumber);
        FormatCountryCode(phoneNumber);
    }

The requirement here is, that if null is passed in, the method will simply do nothing, since there is nothing to format and also not crash with an exception.

Now the problem we are getting is that our SonarQube complains, that this is a test without any Assertions.

Is there any better way to declare a test that tests for a method not doing anything or is this a case where the SonarQube warning should simply be ignored?


Solution

  • I found the solution within FluentAssertions.

    My new code wraps the old call to the _sut in an action and uses Fluent Assertions Should Not Throw mechanism.

    This is an elegant solution because we were using FluentAssertions already anyway. I was just never aware of the Should Not Throw assertion it offers. I only ever knew about Should Throw until now.