rspec/rules/S3431/csharp/how-mstest.adoc

31 lines
755 B
Plaintext
Raw Normal View History

== How to fix it in MSTest
Remove the `ExpectedException` attribute in favor of using the https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.testtools.unittesting.assert.throwsexception[Assert.ThrowsException] assertion.
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))] // Noncompliant
public void Method_NullParam()
{
var sut = new MyService();
sut.Method(null);
}
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
----
[TestMethod]
public void Method_NullParam()
{
var sut = new MyService();
Assert.ThrowsException<ArgumentNullException>(() => sut.Method(null));
}
----