I have a method in SomeClass
, which return IReadOnlyCollection
. Something like that:
public calss SomeClass
{
private readonly List<Part> _parts;
...
public IReadOnlyCollection<Part> GetAllParts =>
this._parts;
}
In my Unit tests I want to assert that returned collection ( expectedCollection
) is IReadOnlyCollection. I have tried with reflection:
[Test]
public void TestWariorsShoudReturnReadOnlyCollectionOfWariors()
{
var expectedCollection = MyPartsLib.GetAllParts;
Type type = expectedCollection.GetType();
string acctualtypeName = type.Name;
string expectedTypeName = "IReadOnlyCollection";
Assert.AreEqual(expectedTypeName,acctualtypeName);
}
But acctualtypeName
after executing is List`1
. How can I assert that expectedCollection
is IReadOnlyCollection
?
Thanks for you help.
You can use is
operator for type checking
[Test]
public void TestWariorsShoudReturnReadOnlyCollectionOfWariors()
{
var expectedCollection = MyPartsLib.GetAllParts;
Assert.True(expectedCollection is IReadOnlyCollection<Part>);
}
You can also use
...
Assert.IsInstanceOfType(expectedCollection, typeof(IReadOnlyCollection<Part>));
...
which results in a clear assertion message pointing out that the wrong type was retrieved.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.