NUnit Error: The Type Initializer for … Threw an Exception (And app.config)
2011-05-27 12:16
831 查看
My Visual Studio solution contains 5 projects, one of them is for unit testing purpose.
The unit testing project is referencing to Entity Framework library where I query data from database. Well, since the Entity Framework is depending on app.config for the connection string, my NUnit returns error when I run it. This is because NUnit can’t find the connection string in app.config file. The error is:
System.TypeInitializationException: The type initializer for ‘…’ threw an exception.
System.ArgumentException: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
Here’s the screenshot (some information is blacked out on purpose). I use NUnit 2.5.9 for unit testing.
Two workarounds for this problem are stated below. You don’t have to apply both workarounds, just either one of them. In my case, the first one works like charm.
Copy app.config file, paste it in the same folder as your .nunit file and rename it to the same as your NUnit project filename.
Example: NUnit project file is “C:/Users/Documents/Nunit/ProjectTest.nunit”. My app.config will have to be “C:/Users/Documents/Nunit/ProjectTest.config”
Copy app.config file, paste it in the same folder as your library (.dll) file and rename it to the same as your library (.dll) filename.
Example: Library project file is “C:/Users/Documents/ProjectSolution/ProjectTest/bin/Debug/ProjectTest.dll”. My app.config will have to be “C:/Users/Documents/ProjectSolution/ProjectTest/bin/Debug/ProjectTest.dll.config” (if doesn’t work, try rename it to ”C:/Users/Documents/ProjectSolution/ProjectTest/bin/Debug/ProjectTest.config”)
The unit testing project is referencing to Entity Framework library where I query data from database. Well, since the Entity Framework is depending on app.config for the connection string, my NUnit returns error when I run it. This is because NUnit can’t find the connection string in app.config file. The error is:
System.TypeInitializationException: The type initializer for ‘…’ threw an exception.
System.ArgumentException: The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
Here’s the screenshot (some information is blacked out on purpose). I use NUnit 2.5.9 for unit testing.
Two workarounds for this problem are stated below. You don’t have to apply both workarounds, just either one of them. In my case, the first one works like charm.
Copy app.config file, paste it in the same folder as your .nunit file and rename it to the same as your NUnit project filename.
Example: NUnit project file is “C:/Users/Documents/Nunit/ProjectTest.nunit”. My app.config will have to be “C:/Users/Documents/Nunit/ProjectTest.config”
Copy app.config file, paste it in the same folder as your library (.dll) file and rename it to the same as your library (.dll) filename.
Example: Library project file is “C:/Users/Documents/ProjectSolution/ProjectTest/bin/Debug/ProjectTest.dll”. My app.config will have to be “C:/Users/Documents/ProjectSolution/ProjectTest/bin/Debug/ProjectTest.dll.config” (if doesn’t work, try rename it to ”C:/Users/Documents/ProjectSolution/ProjectTest/bin/Debug/ProjectTest.config”)
相关文章推荐
- Error creating control: The type initializer for 'Microsoft.Web.Design.ExtenderInformationServiceProvider' threw an exception
- The type initializer for 'TestStatic.StaticClass' threw an exception.
- The type initializer for System.Data.SqlClient.SqlConnection threw an exception
- What Can I do if "The type initializer for 'Emgu.CV.CvInvoke' threw an exception"?
- C#初始化数据库错误提示:The type initializer for 'ASSET.DBHelper' threw an exception
- Could not install the app on the device, read the error above for details. Make sure you have an And
- Could not install the app on the device, read the error above for details. Make sure you have an And
- 报错信息: "System.Web.UI.BaseParser" The type initializer threw an exception.
- The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact
- EF更新时出错,An error occurred while updating the entries. See the inner exception for details
- System.Net.WebException : The remote server returned an error: (415) UNSUPPORTED MEDIA TYPE
- Android An unexpected exception occurred while creating a change object. see the error log for more details
- An error occurred while updating the entries. See the inner exception for details.解决方案
- An exception has occurred while using the formatter ‘JsonMediaTypeFormatter’ to generate sample
- You have an error MySQL server version for the right syntax to use near 'TYPE=InnoDB' at line 20
- C#中异常:“The type initializer to throw an exception(类型初始值设定项引发异常)”的简单分析与解决方法
- Unable to create this part due to an internal errorReason for the failure An unexpected exception was thrown
- C#中异常:“The type initializer to throw an exception(类型初始值设定项引发异常)”的简单分析与解决方法
- An error occurred while updating the entries. See the inner exception for details.
- 解决:ERROR ITMS-90023: "Missing required icon file. The bundle does not contain an app icon for iPad o