Shouldly and Json.NET: This passes?! JToken.Parse("{}").ShouldBe("hello");
Note that the generic type resolution for this gives:
JToken.Parse("{}").ShouldBe<JToken>("hello");
using the implicit conversion operator from string
to JToken
- so we're actually comparing two JToken
s, not a JToken
and a string
.
This fails because JToken : IEnumerable<JToken>
, referring to the sub-tokens. NUnit therefore decides to perform a sequence equality test only. And for both of those, there is no sub-sequence:
JToken x = JToken.Parse("{}");Console.WriteLine(x.Any()); // FalseJToken y = "hello";Console.WriteLine(y.Any()); // False
And two empty sequences are typically considered to be equal.
For info (from metadata, not source):
public abstract class JToken : IJEnumerable<JToken>, System.Collections.Generic.IEnumerable<JToken>, IEnumerable, IJsonLineInfo, ICloneable, IDynamicMetaObjectProvider{ // ... public static implicit operator JToken(string value); // ...}