rspec/rules/S2699/csharp/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

85 lines
1.8 KiB
Plaintext

== Why is this an issue?
A test case without assertions ensures only that no exceptions are thrown. Beyond basic runnability, it ensures nothing about the behavior of the code under test.
This rule raises an exception when no assertions from any of the following frameworks are found in a test:
* `MSTest`
* `NUnit`
* `xUnit`
* `FluentAssertions` (4.x and 5.x)
* `NFluent`
* `NSubstitute`
* `Shoudly`
=== Noncompliant code example
[source,csharp]
----
[TestMethod]
public void MyMethod_WhenSomething_ExpectsSomething()
{
var myClass = new Class();
var result = myClass.GetFoo();
}
----
=== Compliant solution
[source,csharp]
----
[TestMethod]
public void MyMethod_WhenSomething_ExpectsSomething()
{
var myClass = new Class();
var result = myClass.GetFoo();
Assert.IsTrue(result);
}
----
=== Exceptions
To create a custom assertion method declare an attribute with name ``++AssertionMethodAttribute++`` and mark the method with it:
[source,csharp]
----
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class CustomTest
{
[TestMethod]
public void TestMethod1() => Validator.CustomMethod(42); // Compliant
}
public static class Validator
{
[AssertionMethod]
public static void CustomMethod(int value) { }
}
public class AssertionMethodAttribute : Attribute { }
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
=== on 2 May 2019, 14:44:07 Andrei Epure wrote:
This is implemented in https://github.com/SonarSource/sonar-dotnet/blob/7.13.0.8313/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/TestMethodShouldContainAssertion.cs[TestMethodShouldContainAssertion ]
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]