<PackageReference Include="NUnit" Version="3.10.1" />

DictionaryContainsKeyConstraint

DictionaryContainsKeyConstraint is used to test whether a dictionary contains an expected object as a key.
using NUnit.Framework.Internal; using System; using System.Collections; namespace NUnit.Framework.Constraints { public class DictionaryContainsKeyConstraint : CollectionItemsEqualConstraint { public override string DisplayName => "ContainsKey"; public override string Description => "dictionary containing key " + MsgUtils.FormatValue(Expected); protected object Expected { get; set; } public DictionaryContainsKeyConstraint(object expected) : base(expected) { Expected = expected; } protected override bool Matches(IEnumerable actual) { foreach (object key in ConstraintUtils.RequireActual<IDictionary>(actual, "actual", false).Keys) { if (ItemsEqual(key, Expected)) return true; } return false; } public DictionaryContainsKeyConstraint Using<TCollectionType, TMemberType>(Func<TCollectionType, TMemberType, bool> comparison) { Func<TMemberType, TCollectionType, bool> comparison2 = (TMemberType actual, TCollectionType expected) => comparison(expected, actual); Using(EqualityAdapter.For(comparison2)); return this; } } }