rspec/rules/S2970/csharp/rule.adoc

76 lines
2.1 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
It is very easy to write incomplete assertions when using some test frameworks. This rule enforces complete assertions in the following cases:
* 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.
2020-06-30 12:48:07 +02:00
=== Noncompliant code example
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:48:07 +02:00
----
string actual = "Hello World!";
// Fluent Assertions
actual.Should(); // Noncompliant
// NFluent
Check.That(actual); // Noncompliant
// NSubstitute
command.Received(); // Noncompliant
2020-06-30 12:48:07 +02:00
----
=== Compliant solution
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:48:07 +02:00
----
string actual = "Hello World!";
// Fluent Assertions
2020-06-30 12:48:07 +02:00
actual.Should().Contain("Hello");
// NFluent
Check.That(actual).Contains("Hello");
// NSubstitute
command.Received().Execute();
2020-06-30 12:48:07 +02:00
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
=== on 24 Nov 2015, 14:01:46 Ann Campbell wrote:
\[~tamas.vajk] see truncated description in main task.
=== on 24 Nov 2015, 14:27:15 Tamas Vajk wrote:
\[~ann.campbell.2] It seems okay.
Do we mark somehow rules that are library dependent? For example, out of the box we only have ``++Assert.IsTrue++`` and ``++Assert.AreEqual++``-like assertions. With those, this rule doesn't make sense. But when you add a library like "FluentAssertions", you could write "something.Should();" instead of "something.Should().NotBeNull()".
=== on 24 Nov 2015, 14:36:37 Ann Campbell wrote:
\[~tamas.vajk] should we expand the examples to include:
----
Assert; // Noncompliant
----
----
Assert.AreNotSame(actual,"");
----
? Does the noncompliant version compile?
=== on 24 Nov 2015, 14:49:24 Tamas Vajk wrote:
\[~ann.campbell.2] No, ``++Assert;++`` on its own doesn't compile.
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]