Skip to content

NUnit2022

Mikkel Nylander Bundgaard edited this page Apr 25, 2020 · 2 revisions

NUnit2022

Missing property required for constraint.

Topic Value
Id NUnit2022
Severity Warning
Enabled True
Category Assertion
Code MissingPropertyAnalyzer

Description

Provided 'actual' argument should have required property for constraint.

Motivation

Using property constraints (e.g. Has.Count.EqualTo(1), Has.Property("Prop").EqualTo(expected), etc) makes sense only when provided actual argument has those properties defined.

[Test]
public void Test()
{
    var enumerable = new [] {1,2,3}.Where(i => i > 1);

    // Actual argument type 'IEnumerable<int>' has no property 'Count'.
    Assert.That(enumerable, Has.Count.EqualTo(2));
}

How to fix violations

Fix your property name, or use another constraint.

Configure severity

Via ruleset file.

Configure the severity per project, for more info see MSDN.

Via #pragma directive.

#pragma warning disable NUnit2022 // Missing property required for constraint.
Code violating the rule here
#pragma warning restore NUnit2022 // Missing property required for constraint.

Or put this at the top of the file to disable all instances.

#pragma warning disable NUnit2022 // Missing property required for constraint.

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion", 
    "NUnit2022:Missing property required for constraint.",
    Justification = "Reason...")]
Clone this wiki locally