rspec/rules/S2699/csharp/rule.adoc

80 lines
1.6 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2020-06-30 12:48:07 +02:00
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.
2021-02-02 15:02:10 +01:00
2020-06-30 12:48:07 +02:00
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`
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
----
[TestMethod]
public void MyMethod_WhenSomething_ExpectsSomething()
{
var myClass = new Class();
var result = myClass.GetFoo();
}
----
=== 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
----
[TestMethod]
public void MyMethod_WhenSomething_ExpectsSomething()
{
var myClass = new Class();
var result = myClass.GetFoo();
Assert.IsTrue(result);
}
----
=== Exceptions
2021-01-27 13:42:22 +01:00
To create a custom assertion method declare an attribute with name ``++AssertionMethodAttribute++`` and mark the method with it:
2021-02-02 15:02:10 +01:00
----
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)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]