Is Assert.DoesNotContain(object, IEnumerable)
supposed to call the GetEnumerator
of items in collection?
#2611
-
I'm hitting this weird issue with using I'll do my best to explain what's going on here. Let's say I have class public class DataField : IEnumerable
{
private List<Subfield> _subfields = new();
public string Tag { get; }
public char Indicator1 { get; }
public char Indicator2 { get; }
public DataField(string tag, char indicator1, char indicator2, List<Subfield> subfields) =>
(Tag, Indicator1, Indicator2, _subfields) = (tag, indicator1, indicator2, subfields);
public IEnumerator GetEnumerator() => _subfields.GetEnumerator();
// Explicit override of ToString
public override string ToString()
{
StringBuilder builder = new();
builder.Append(Tag).Append(" ");
builder.Append(Indicator1).Append(" ");
builder.Append(Indicator2).Append(" ");
builder.AppendJoin(' ', _subfields);
return builder.ToString();
}
// Expclit override of Equals using ToString() value to check for equality
public override bool Equals(object? obj)
{
if(obj is DataField other)
{
return this.ToString() == other.ToString();
}
return false;
}
public override int GetHashCode() => this.ToString().GetHashCode();
} The public class Subfield
{
public char Code { get; }
public string Value { get; }
public Subfield(char code, string value) => (Code, Value) = (code, value);
public override string ToString() => $"{Code}| {Value}";
} So I make two instances of List<Subfield> subfields = new();
subfields.Add(new('a', "Test"));
DataField field001 = new("001", '1', '2', subfields);
DataField field999 = new("999", '1', '2', subfields); Then, i add only List<DataField> actual = new() { field001 }; Now I want to Assert that the collection does not contain Assert.DoesNotContain<DataField>(field999, actual); My expectation is that this would pass. However, it is not passing at all. Instead, what is happen is the You can see this in the output in the console
The My question is, is the called to |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Looks like an issue (Issue #2612 )was opened for this because of my confusion, but I'll mention that the response on twitter suggested using |
Beta Was this translation helpful? Give feedback.
Looks like an issue (Issue #2612 )was opened for this because of my confusion, but I'll mention that the response on twitter suggested using
IEquatable<T>
to resolve this for now.https://twitter.com/xunit/status/1586419335207464961