bbsimonbb
bbsimonbb

Reputation: 29020

xunit testing web api 2 controller : throws Xunit.Sdk.IsTypeException

I wan't to test the insert method on a a web api controller. The method returns a CreatedNegotiatedContentResult. Everything goes fine up to the Assert...

Assert.IsType(typeof(CreatedNegotiatedContentResult<Justif>), result);

and boom...

Xunit.Sdk.IsTypeException was unhandled by user code
  Actual=System.Web.Http.Results.CreatedNegotiatedContentResult`1[[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
  ActualTitle=Actual
  Expected=System.Web.Http.Results.CreatedNegotiatedContentResult`1[[HubHacienda.Justif, HubHacienda, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
  ExpectedTitle=Expected
  HResult=-2146233088
  Message=Assert.IsType() Failure
Expected: System.Web.Http.Results.CreatedNegotiatedContentResult`1[[HubHacienda.Justif, HubHacienda, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Actual:   System.Web.Http.Results.CreatedNegotiatedContentResult`1[[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
  Source=xunit.assert
  UserMessage=Assert.IsType() Failure
  StackTrace:
       at Xunit.Assert.IsType(Type expectedType, Object object) in C:\BuildAgent\work\cb37e9acf085d108\src\xunit.assert\Asserts\TypeAsserts.cs:line 99
       at HubHacienda.Tests.JustifsControllerTest.Post_WhenModelValid_ShouldReturnSuccess() in C:\NotilusTNE\Sources\Hub\Hub Hacienda\Dev\HubHacienda\HubHacienda.Tests\JustifsControllerTests.cs:line 52
  InnerException: 

This is my test framework blowing up? This can't be right?

Upvotes: 1

Views: 1258

Answers (1)

bbsimonbb
bbsimonbb

Reputation: 29020

Ok this appears to be an xunit bug. The problem is I'm comparing a generic type, of the form SystemType<MyType>. The typeof() operator in the xunit project returns a strong name indicating my dll, whereas result.GetType() returns a strong name with the system dll.

The workaround is to compare just the Type.Name...

System.Type expectation = typeof(System.Web.Http.Results.CreatedNegotiatedContentResult<Justif>);
System.Type tristeRealite = result.GetType();
Assert.Equal(expectation.Name, tristeRealite.Name);

Upvotes: 1

Related Questions