Search Results for

    Show / Hide Table of Contents

    NUnit2009

    The same value has been provided as both the actual and the expected argument

    Topic Value
    Id NUnit2009
    Severity Warning
    Enabled True
    Category Assertion
    Code SameActualExpectedValueAnalyzer

    Description

    The same value has been provided as both the actual and the expected argument. This indicates a coding error.

    Motivation

    To bring developers' attention to a situation in which their code may not be operating as expected and their test may not be testing what they expect.

    How to fix violations

    Sample Violation

    [Test]
    public void NUnit2009SampleTest()
    {
        var x = 1;
        Assert.That(x, Is.EqualTo(x));
    }
    

    Explanation

    In the above example, the test will always be correct, because we're comparing the same value. That is to say, we're not actually testing anything.

    Fix

    Ensure the expected and actual values come from different places.

    [Test]
    public void NUnit2009SampleTest()
    {
        var x = 1;
        Assert.That(x, Is.EqualTo(1));
    }
    

    Configure severity

    Via ruleset file

    Configure the severity per project, for more info see MSDN.

    Via .editorconfig file

    # NUnit2009: The same value has been provided as both the actual and the expected argument
    dotnet_diagnostic.NUnit2009.severity = chosenSeverity
    

    where chosenSeverity can be one of none, silent, suggestion, warning, or error.

    Via #pragma directive

    #pragma warning disable NUnit2009 // The same value has been provided as both the actual and the expected argument
    Code violating the rule here
    #pragma warning restore NUnit2009 // The same value has been provided as both the actual and the expected argument
    

    Or put this at the top of the file to disable all instances.

    #pragma warning disable NUnit2009 // The same value has been provided as both the actual and the expected argument
    

    Via attribute [SuppressMessage]

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion",
        "NUnit2009:The same value has been provided as both the actual and the expected argument",
        Justification = "Reason...")]
    
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0