137 lines
3.8 KiB
Plaintext
137 lines
3.8 KiB
Plaintext
== Why is this an issue?
|
|
|
|
This rule addresses the issue of incomplete assertions that can occur when using certain test frameworks. Incomplete assertions can lead to tests that do not effectively verify anything. The rule enforces the use of complete assertions in specific cases, namely:
|
|
|
|
* Fluent Assertions: https://fluentassertions.com/introduction[Should()] is not followed by an assertion invocation.
|
|
[source,csharp]
|
|
----
|
|
string actual = "Using Fluent Assertions";
|
|
actual.Should(); // Noncompliant
|
|
----
|
|
* NFluent: https://www.n-fluent.net[Check.That()] is not followed by an assertion invocation.
|
|
[source,csharp]
|
|
----
|
|
string actual = "Using NFluent";
|
|
Check.That(actual); // Noncompliant
|
|
----
|
|
* NSubstitute: https://nsubstitute.github.io/help/received-calls[Received()] is not followed by an invocation.
|
|
[source,csharp]
|
|
----
|
|
command.Received(); // Noncompliant
|
|
----
|
|
|
|
In such cases, what is intended to be a test doesn't actually verify anything.
|
|
|
|
== How to fix it in Fluent Assertions
|
|
|
|
`Fluent Assertions` provides an interface for writing assertions, and it is important to ensure that `Should()` is properly used in conjunction with an assertion method.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
string actual = "Hello World!";
|
|
actual.Should(); // Noncompliant
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
|
----
|
|
string actual = "Hello World!";
|
|
actual.Should().Contain("Hello");
|
|
----
|
|
|
|
== How to fix it in NFluent
|
|
|
|
`NFluent` offers a syntax for assertions, and it's important to follow `Check.That()` with an assertion method to complete the assertion.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,csharp,diff-id=2,diff-type=noncompliant]
|
|
----
|
|
string actual = "Hello World!";
|
|
Check.That(actual); // Noncompliant
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,csharp,diff-id=2,diff-type=compliant]
|
|
----
|
|
string actual = "Hello World!";
|
|
Check.That(actual).Contains("Hello");
|
|
----
|
|
|
|
== How to fix it in NSubstitute
|
|
|
|
`NSubstitute` is a mocking framework, and `Received()` is used to verify that a specific method has been called.
|
|
However, invoking a method on the mock after calling `Received()` is necessary to ensure the complete assertion.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,csharp,diff-id=3,diff-type=noncompliant]
|
|
----
|
|
command.Received(); // Noncompliant
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,csharp,diff-id=3,diff-type=compliant]
|
|
----
|
|
command.Received().Execute();
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* https://fluentassertions.com/introduction[Fluent assertions: Should()]
|
|
* https://www.n-fluent.net[NFluent: Check.That()]
|
|
* https://nsubstitute.github.io/help/received-calls[NSubstitute: Received()]
|
|
|
|
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[]
|