Unit Tests - Referencing 3.0.0.0 MVC, version actually 4.0.0.0

553 Views Asked by At

I am testing SendEmail using Unit Tests (Nunit):

// Arrange
var mockDbContext = new Mock<DbContext>();
IService service = new Service(mockDbContext.Object);

// Act
var result = service.SendEmail(string.Empty,1,1);

//Assert
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.InstanceOf<bool>());
Assert.AreEqual(result, false);   

public bool SendEmail(string emailAddress, int mId, int deadline)
{
    try
    {
        dynamic email = new Email("CirculationEmail");
        email.To = emailAddress;
        email.MId = mId;
        email.Deadline = deadline;
        email.Send();

        return true;
    }
    catch (Exception ex)
    {
        Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
        return false;
    }
}

SendEmail uses Postal. I get the following error when I run the test. When I debug it throws the error before even hitting the first debug point within my SendEmail.

System.IO.FileLoadException : Could not load file or assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I added the below to the app.config and searched through my code to see if it ever references 3.0.0.0, it doesn't.

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

I am wondering if maybe another error message is being masked (somehow) by the MVC wrong version one?

3

There are 3 best solutions below

0
On

A couple of options:

There are a few ways to find out where assembly is loaded from. Best one is to use Sysinternal's ProcessMonitor to see which file it is loading before the error happens.

Also try to check in your project if the assembly reference is configured as an "exact match".In your project "References" folder select the reference, go to "Properties" and change "Specific Version" to false.

0
On

The only times I've had this, I'd had the correct version referenced in the project i was testing - but the unit test project didn't have the correct references.

Ensure that the references are correct in the unit test project, as well as the tested project.

0
On

I had the same error. I tried updating "System.Web.Mvc" package from NuGet package manager in the unit test project. But it didn't reflect any change in references to the unit test project. Heres my solution:

  • I opened the main project references and double clicked "System.Web.Mvc" package which led to the view in the object browser.
  • Then I just copied that path of a file opened it in file explorer. where I found dll file of "System.Web.Mvc" package.
  • I did the same procedure for the unit test project where I found dll file of "System.Web.Mvc" package of an older version.
  • I replaced it with dll file of "System.Web.Mvc" package found from main project.

It worked. Though it's not proper way for upgrading pages but its quick.