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

DictionaryContainsKeyConstraint

DictionaryContainsKeyConstraint is used to test whether a dictionary contains an expected object as a key.
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) { IDictionary obj = actual as IDictionary; if (obj == null) throw new ArgumentException("The actual value must be an IDictionary", "actual"); foreach (object key in obj.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; } } }