System.TypeLoadException: Could not load type 'x' from assembly 'x'?

Ben picture Ben · Apr 28, 2014 · Viewed 7.3k times · Source

I would just like to apologise by the overload of information, I just want to provide anything that could be related, I'm not sure what's happening

Test method DCIM_Test_1.MySQLDefTest.ConnectionStringTest threw exception: System.TypeLoadException: Could not load type 'DCIM_With_Test.Database.Database_MySQLDef' from assembly 'DCIM, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

I am getting this error when I run one of my tests, and I believe it's coming from how I re-named my Solution and Projects recently.

I renamed them all from 'DCIM_With_Test' to 'DCIM', (Changed the directories, file names etc.) but it is still causing inconsistencies here and there.

Another cause could possibly be the way the method that is causing the issue is running. I have changed the method from reading from a text file to reading from an app.config file and I am new to this process and XML so I could be missing a step somewhere.

Here is my app.config:

<?xml version="1.0"?>
<configuration>
<startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
  <connectionStrings>
    <add name="mySqlConnectionString" connectionString="server=localhost;database=dcim;uid=root;pwd=LlmD62jL;"/>
  </connectionStrings>
</configuration>

I am also getting three messages about app.config:

  1. Could not find the schema information for the element 'supportedRuntime'.
  2. Could not find the schema information for the attribute 'version'.
  3. Could not find the schema information for the attribute 'sku'.

Here is the method being tested NOTE: The OLD METHOD runs fine, but the test still throws the exception, but the NEW METHOD doesn't work at all.

public static string GetConnectionString()
{
    /* OLD METHOD
    StreamReader rdr = new StreamReader(@"C:\Users\Benjamin\Documents\setupfile.txt");
    mySqlConnectionString = rdr.ReadToEnd();
    rdr.Close();
    */

    // NEW METHOD    
    mySqlConnectionString = ConfigurationManager.ConnectionStrings["mySqlConnectionString"].ConnectionString;

    return mySqlConnectionString;
}

And here is the test:

[TestMethod()]
public void ConnectionStringTest()
{
    int actual = 0;
    int expected = 1;
    Database_MySQLDef.GetConnectionString();
    if (Database_MySQLDef.mySqlConnectionString != "")
    {
        actual = 1;
    }
    else { actual = 0; }

    Assert.AreEqual(expected, actual);
}

Answer

Mike Zboray picture Mike Zboray · Apr 29, 2014

Does your test assembly have an app.config?

my test project doesn't but my main project does. Not sure if that's the same thing?

No, they aren't. I suspect what is happening is that ConfigurationManager.ConnectionStrings is going to the test assembly's app config (this is MSTest's behavior, I believe), not the your main project's app config. Then, because that doesn't exist, you are getting an Exception thrown in a static initializer or static constructor and this is wrapped by the TypeLoadException.

A simple way to solve this is to just add an app.config to your test assembly. Then you can put in whatever connection string you need for the tests.