IsType<T> and IsType(object, object) throwing IsTypeException

倖福魔咒の 提交于 2020-01-03 08:31:36

问题


I am attempting to assert that an object being returned by a method call is of the type List<MyClass>, so using xUnit I have tried the following:

var expected = typeof(List<MyClass>);
var actual = typeof(method());

Assert.IsType<List<MyClass>>(actual);
Assert.IsType(expected, actial);

Both of the above throw the IsTypeException however if I perform:

var areSameType = expected == actual

areSameType is true. So is there something going on deeper down that I am not accounting for?

Docs:

http://www.nudoq.org/#!/Packages/xunit.extensions/xunit.extensions/Assertions/M/IsType(T) http://www.nudoq.org/#!/Packages/xunit.extensions/xunit.extensions/Assertions/M/IsType


回答1:


The input for Assert.IsType should be the object itself not its type, the following should not throw:

var expected = typeof(List<MyClass>);
var actual = Method();

Assert.IsType<List<MyClass>>(actual);
Assert.IsType(expected, actual);


来源:https://stackoverflow.com/questions/26898553/istypet-and-istypeobject-object-throwing-istypeexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!