问题
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:
- Could not find the schema information for the element 'supportedRuntime'.
- Could not find the schema information for the attribute 'version'.
- 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);
}
回答1:
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.
来源:https://stackoverflow.com/questions/23334933/system-typeloadexception-could-not-load-type-x-from-assembly-x