Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error unit testing webapi controller

I'm using AspNet Web Api Client 5.0 and i am trying to unit test a web api controller.

var encservice = new EncryptionService();
var acctservice = FakeServices.GetAccountService();
var controller = new AccountController(acctservice, encservice);
controller.Request = new HttpRequestMessage();

when the code

controller.Request.SetConfiguration(new HttpConfiguration());

is executed i hit an exception

Message: Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Source: System.Net.Http.Formatting

Stacktrace: at System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor() at System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters() at System.Net.Http.Formatting.MediaTypeFormatterCollection..ctor() at System.Web.Http.HttpConfiguration.DefaultFormatters() at System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes) at System.Web.Http.HttpConfiguration..ctor() at EMR.Test.Controller.AccountControllerTest.Should_Get() in c:\PremiumProjectsCollection\emr\src\EMRAzure\EMRAzure\EMR.Test\Controller\AccountControllerTest.cs:line 34

the version of newsoft.json that i am using is 6.0

I also have a assembly redirection in my confguration file

 <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
  </dependentAssembly>

The test runner that im using is MStest, VS2012

like image 844
reggieboyYEAH Avatar asked Mar 04 '14 06:03

reggieboyYEAH


2 Answers

You'll need to add an assembly redirect:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json"
                          publicKeyToken="30ad4fe6b2a6aeed"
                          culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

(assuming that the assembly version of Newtonsoft.Json is exactly 6.0.0.0.)

like image 127
Mark Seemann Avatar answered Sep 23 '22 03:09

Mark Seemann


(note comment below is referring to a Web Api Client project having this issue)

I had the same problem with the version of Newtonsoft.Json so I deleted the older version references and used Package Manager Console to install the latest version of Newtonsoft.Json on my Web Api Client Library and Test Project.

Install-Package Newtonsoft.Json -Version 6.0.8 (note u might need to find out which is the latest version)

The problem remained so I realized there is a crash between System.Net.Http.Formatting and my latest version of Json. To solve this, delete the System.Net.Http and System.Net.Http.Formatting references and install the WebApi Client Library via Nuget instead, as below:

Install-Package Microsoft.AspNet.WebApi.Client

This solved it for me.

like image 20
niovi Avatar answered Sep 24 '22 03:09

niovi