rspec/rules/S2699/csharp/rule.adoc

59 lines
1.3 KiB
Plaintext
Raw Normal View History

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:
2021-01-27 13:42:22 +01:00
* ``++MSTest++``
* ``++NUnit++``
* ``++XUnit++``
* ``++FluentAssertions++`` (4.x and 5.x)
* ``++NSubstitute++``
2020-06-30 12:48:07 +02:00
== Noncompliant Code Example
----
[TestMethod]
public void MyMethod_WhenSomething_ExpectsSomething()
{
var myClass = new Class();
var result = myClass.GetFoo();
}
----
== Compliant Solution
----
[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 { }
----