Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3415

Assertion arguments should be passed in the correct order

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Swap these 2 arguments so they are in the correct order: expected value, actual value.
    • Highlighting:
      • Primary: second argument to swap
      • Secondary: first argument to swap
        • message: 'Other argument to swap.'
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      C#, Java, PHP
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • Analysis Scope:
      Test Sources
    • Implementation details:

      Description

      The standard assertions library methods such as AreEqual and AreSame in MSTest and NUnit, or Equal and Same in XUnit, expect the first argument to be the expected value and the second argument to be the actual value. Swap them, and your test will still have the same outcome (succeed/fail when it should) but the error messages will be confusing.

      This rule raises an issue when the second argument to an assertions library method is a hard-coded value and the first argument is not.

      Noncompliant Code Example

      Assert.AreEqual(runner.ExitCode, 0, "Unexpected exit code"); // Noncompliant; Yields error message like: Expected:<-1>. Actual:<0>.
      

      Compliant Solution

      Assert.AreEqual(0, runner.ExitCode, "Unexpected exit code");
      

        Attachments

          Issue Links

          1.
          Java RSPEC-4237 Language-Specification Active Unassigned
          2.
          PHP RSPEC-5920 Language-Specification Active Unassigned
          3.
          Javascript RSPEC-6089 Language-Specification Active Unassigned

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              ann.campbell.2 Ann Campbell
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated: