[英]How to assert that collection is IReadOnlyCollection using NUnit
我在SomeClass
有一个方法,它返回IReadOnlyCollection
。 像这样的东西:
public calss SomeClass
{
private readonly List<Part> _parts;
...
public IReadOnlyCollection<Part> GetAllParts =>
this._parts;
}
在我的单元测试中,我想断言返回的集合( expectedCollection
)是 IReadOnlyCollection。 我尝试过反思:
[Test]
public void TestWariorsShoudReturnReadOnlyCollectionOfWariors()
{
var expectedCollection = MyPartsLib.GetAllParts;
Type type = expectedCollection.GetType();
string acctualtypeName = type.Name;
string expectedTypeName = "IReadOnlyCollection";
Assert.AreEqual(expectedTypeName,acctualtypeName);
}
但是执行后的acctualtypeName
是List`1
。 我如何断言expectedCollection
是IReadOnlyCollection
?
谢谢你的帮助。
您可以使用is
运算符进行类型检查
[Test]
public void TestWariorsShoudReturnReadOnlyCollectionOfWariors()
{
var expectedCollection = MyPartsLib.GetAllParts;
Assert.True(expectedCollection is IReadOnlyCollection<Part>);
}
你也可以使用
...
Assert.IsInstanceOfType(expectedCollection, typeof(IReadOnlyCollection<Part>));
...
这会产生一个明确的断言消息,指出检索到了错误的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.