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
- is implemented by
-
SONARJAVA-2422 Rule S3415: Assertion arguments should be passed in the correct order
-
- Closed
-
-
SONARPHP-989 Add rule S3415: Assertion arguments should be passed in the correct order
-
- Closed
-
-
SONARJAVA-3523 Extend S3415 (Arguments order) to support TestNG assertions
-
- Closed
-
-
SONARJAVA-3590 S3415: Add a message on secondary locations
-
- Closed
-
- is related to
-
SONARJAVA-3922 Add quick fixes for S3415 (AssertionArgumentOrderCheck)
-
- Closed
-
-
SONARJAVA-3401 Extend S3415 (Arguments order) to support AssertJ assertions
-
- Closed
-
-
SONARJAVA-3431 S3415: better support of constant used as actual value
-
- Closed
-
- relates to
-
SONARPHP-1100 S3415: Add a message on secondary locations
-
- Closed
-
- links to
1.
|
Java | RSPEC-4237 |
|
Active | Unassigned | |
2.
|
PHP | RSPEC-5920 |
|
Active | Unassigned | |
3.
|
Javascript | RSPEC-6089 |
|
Active | Unassigned |