o[0].AccountEnabled
具有类型
bool
,而
graphUsers[0].AccountEnabled
具有类型
bool?
。没有明确施放到
bool?
,我会收到以下错误:
'bool' does not contain a definition for 'ShouldBe' and the best extension method overload 'ShouldBeTestExtensions.ShouldBe<bool?>(bool?, bool?, string?)' requires a receiver of type 'bool?'CS1929
我应该如何使用应该使用应使用的不可删除和无效类型的值? xunit.net使用
Assert.Equal()
没有问题我更喜欢在每次这种比较的情况下不投射扩展方法接收器。 Furthermore,它甚至如何与无效的铸件(
(bool?)
)一起使用?
ShouldBe
的定义是:
public static void ShouldBe<T>(
[NotNullIfNotNull(nameof(expected))] this T? actual,
[NotNullIfNotNull(nameof(actual))] T? expected,
string? customMessage = null)
{
if (ShouldlyConfiguration.CompareAsObjectTypes.Contains(typeof(T).FullName!) || typeof(T) == typeof(string))
actual.AssertAwesomely(v => Is.Equal(v, expected, new ObjectEqualityComparer<T>()), actual, expected, customMessage);
else
actual.AssertAwesomely(v => Is.Equal(v, expected), actual, expected, customMessage);
}
,如果是T
是
bool?
,那么它将接受类型
bool??
的参数,这不是有效的类型。
代码:
owners.ShouldSatisfyAllConditions(
o => o.Count.ShouldBe(graphUsers.Count),
o => o[0].Id.ShouldBe(Guid.Parse(graphUsers[0].Id!)),
o => o[0].DisplayName.ShouldBe(graphUsers[0].DisplayName),
o => o[0].GivenName.ShouldBe(graphUsers[0].GivenName),
o => o[0].Surname.ShouldBe(graphUsers[0].Surname),
o => o[0].UserPrincipalName.ShouldBe(graphUsers[0].UserPrincipalName),
o => o[0].Email.ShouldBe(graphUsers[0].Mail),
o => ((bool?)o[0].AccountEnabled).ShouldBe(graphUsers[0].AccountEnabled)
);
我真的不应该知道,但是如果您不想使用C#的内置铸造语法。 this this Estes通过:
ShouldBeAssignableTo
我本人可能更喜欢第二个选项。