From 49d9b8f5a55a7dc4d7e5957bb7d66b3f3ce6198c Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Wed, 25 Jan 2023 13:06:40 +0100 Subject: [PATCH 1/4] Add documentation for analyzed C# testing libraries --- rules/S2970/csharp/rule.adoc | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/rules/S2970/csharp/rule.adoc b/rules/S2970/csharp/rule.adoc index 99dbde1b75d..56d062a5a31 100644 --- a/rules/S2970/csharp/rule.adoc +++ b/rules/S2970/csharp/rule.adoc @@ -1,11 +1,25 @@ -include::../description.adoc[] +It is very easy to write incomplete assertions when using some test frameworks. This rule enforces complete assertions in the following cases: + +* MSTest: https://learn.microsoft.com/dotnet/api/microsoft.visualstudio.testtools.unittesting.assert.that[`Assert.That`] is not followed by an assertion invocation (also https://learn.microsoft.com/dotnet/api/microsoft.visualstudio.testtools.unittesting.stringassert.that[`StringAssert.That`] and https://learn.microsoft.com/dotnet/api/microsoft.visualstudio.testtools.unittesting.collectionassert.that[`CollectionAssert.That`]). +* Fluent Assertions: https://fluentassertions.com/introduction[`Should()`] is not followed by an assertion invocation. +* NFluent: https://www.n-fluent.net[`Check.That()`] is not followed by an assertion invocation +* NSubstitute: https://nsubstitute.github.io/help/received-calls[`Received()`] is not followed by an invocation + +In such cases, what is intended to be a test doesn't actually verify anything. == Noncompliant Code Example [source,csharp] ---- string actual = "Hello World!"; -actual.Should(); // Noncompliant +// MSTest +StringAssert.That; // Noncompliant +// Fluent Assertions +actual.Should(); // Noncompliant +// NFluent +Check.That(actual); // Noncompliant +// NSubstitute +command.Received(); // Noncompliant ---- == Compliant Solution @@ -13,7 +27,14 @@ actual.Should(); // Noncompliant [source,csharp] ---- string actual = "Hello World!"; +// MSTest +StringAssert.That.Contains(actual, "Hello"); +// Fluent Assertions actual.Should().Contain("Hello"); +// NFluent +Check.That(actual).Contains("Hello"); +// NSubstitute +command.Received().Execute(); ---- ifdef::env-github,rspecator-view[] From 06159c046316bbd4c698271c90b655b9a0772f1d Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Wed, 25 Jan 2023 13:18:03 +0100 Subject: [PATCH 2/4] Remove unused file --- rules/S2970/description.adoc | 1 - 1 file changed, 1 deletion(-) delete mode 100644 rules/S2970/description.adoc diff --git a/rules/S2970/description.adoc b/rules/S2970/description.adoc deleted file mode 100644 index 63e9c6f0174..00000000000 --- a/rules/S2970/description.adoc +++ /dev/null @@ -1 +0,0 @@ -It is very easy to write incomplete assertions when using some test frameworks. This rule raises an issue when an assertion doesn't actually test anything. \ No newline at end of file From 376c93f1e821efbff87de6fb160f37579ac690b3 Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Fri, 27 Jan 2023 09:29:25 +0100 Subject: [PATCH 3/4] Add dots --- rules/S2970/csharp/rule.adoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rules/S2970/csharp/rule.adoc b/rules/S2970/csharp/rule.adoc index 56d062a5a31..e1d61e3e391 100644 --- a/rules/S2970/csharp/rule.adoc +++ b/rules/S2970/csharp/rule.adoc @@ -2,8 +2,8 @@ It is very easy to write incomplete assertions when using some test frameworks. * MSTest: https://learn.microsoft.com/dotnet/api/microsoft.visualstudio.testtools.unittesting.assert.that[`Assert.That`] is not followed by an assertion invocation (also https://learn.microsoft.com/dotnet/api/microsoft.visualstudio.testtools.unittesting.stringassert.that[`StringAssert.That`] and https://learn.microsoft.com/dotnet/api/microsoft.visualstudio.testtools.unittesting.collectionassert.that[`CollectionAssert.That`]). * Fluent Assertions: https://fluentassertions.com/introduction[`Should()`] is not followed by an assertion invocation. -* NFluent: https://www.n-fluent.net[`Check.That()`] is not followed by an assertion invocation -* NSubstitute: https://nsubstitute.github.io/help/received-calls[`Received()`] is not followed by an invocation +* NFluent: https://www.n-fluent.net[`Check.That()`] is not followed by an assertion invocation. +* NSubstitute: https://nsubstitute.github.io/help/received-calls[`Received()`] is not followed by an invocation. In such cases, what is intended to be a test doesn't actually verify anything. From e3f7cef87949323a7303cb907aefc398a225df7a Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Wed, 22 Feb 2023 11:35:58 +0100 Subject: [PATCH 4/4] Remove MSTest (Properties can not be used as a statement) --- rules/S2970/csharp/rule.adoc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/rules/S2970/csharp/rule.adoc b/rules/S2970/csharp/rule.adoc index e1d61e3e391..59320137efd 100644 --- a/rules/S2970/csharp/rule.adoc +++ b/rules/S2970/csharp/rule.adoc @@ -1,6 +1,5 @@ It is very easy to write incomplete assertions when using some test frameworks. This rule enforces complete assertions in the following cases: -* MSTest: https://learn.microsoft.com/dotnet/api/microsoft.visualstudio.testtools.unittesting.assert.that[`Assert.That`] is not followed by an assertion invocation (also https://learn.microsoft.com/dotnet/api/microsoft.visualstudio.testtools.unittesting.stringassert.that[`StringAssert.That`] and https://learn.microsoft.com/dotnet/api/microsoft.visualstudio.testtools.unittesting.collectionassert.that[`CollectionAssert.That`]). * Fluent Assertions: https://fluentassertions.com/introduction[`Should()`] is not followed by an assertion invocation. * NFluent: https://www.n-fluent.net[`Check.That()`] is not followed by an assertion invocation. * NSubstitute: https://nsubstitute.github.io/help/received-calls[`Received()`] is not followed by an invocation. @@ -12,8 +11,6 @@ In such cases, what is intended to be a test doesn't actually verify anything. [source,csharp] ---- string actual = "Hello World!"; -// MSTest -StringAssert.That; // Noncompliant // Fluent Assertions actual.Should(); // Noncompliant // NFluent @@ -27,8 +24,6 @@ command.Received(); // Noncompliant [source,csharp] ---- string actual = "Hello World!"; -// MSTest -StringAssert.That.Contains(actual, "Hello"); // Fluent Assertions actual.Should().Contain("Hello"); // NFluent