Unit test causes exception "The type initializer for 'XXX' threw an exception" when running code which accesses appSettings of the webconfig

I have a method which I want to unit test, the code inside that method accesses the appSetting to get the connectionString 

: System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString();

this piece of code works fine , but when  I m doing the unit test to test the method, the above line gives the below exception :

MyMethod threw exception:  System.TypeInitializationException: The type initializer for 'XXX' threw an exception. --->  System.NullReferenceException: Object reference not set to an instance of an object..   

 Any ideas?

 

0
lordofthexings
4/2/2009 6:41:06 PM
asp.net.config-deploy 12495 articles. 0 followers. Follow

3 Replies
426 Views

Similar Articles

[PageSpeed] 35

just a thought (might not be it at all)...

does you test config file have an app settings section?
i use MbUnit with TestDriven.Net add in for VS and I have to use a PostBuildEvent script to get my "test.config" file in the bid/Debug directory with the correct file name (the DLL name + ".config")

again... don't know if that is it but...

hope that helps

0
mmeisinger
4/3/2009 11:07:01 PM

Hi,

Are you testing a class that accesses web.config using Unit Test? If so, the object of web.config is unavailable.

In this case, you can try to create a XML file to replace web.config file and modify source code to access this xml file temporary.

Additionally, you can consider using VSTS web testing to test ASP.NET application (http://msdn.microsoft.com/en-us/library/ms182409(VS.80).aspx). You also can search another web test tool on internet.

 

I look forward to receiving your test results.


Thomas Sun
Microsoft Online Community Support

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
0
Thomas
4/7/2009 8:38:52 AM

>You also can search another web test tool on internet.

There is now WaTiN  at  http://watin.sourceforge.net/


Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
0
TATWORTH
4/7/2009 11:13:24 PM
Reply:

Similar Artilces:

Regarding exception -Message "The type initializer for "eradllc.maindbtest.DataProvider" threw an exception." String
Hi I am facing following exception when developing a module ie,                       Message               "The type initializer for "eradllc.maindbtest.DataProvider" threw an exception."          String       .This is when I want to send two string values(textbox values with a button click)  to database. I am sending my code .my namespace is eradllc.maind...

Module development of DNN i am facing following issue -Exception Message "The type initializer for "eradllc.maindbtest.DataProvider" threw an exception." String
 Message "The type initializer for "eradllc.maindbtest.DataProvider" threw an exception." String   in this   eradllc.maindbtest as my namespace .i found issue by inserting two string values to database.provider name is correct. it is generating from   createprovider().the dataprovider,namespace,assembly name all are correct.   pls help  NavinAbstract...

Replace(txt,"'","''") in code-behind
i'm creating a file named client.vb using my wordpad. I'm using this as a code-behind sort of thing. The problem is I want to remove the ' from my strings so that i can put them in SQL statements. The Replace doesn't work, is there any other way to this. thanks. Can you show me the code sample you have for the string.Replace() method? That should work for you. You should instead use Sql Parameters to compile and send your query to the db. One of the many benefits of doing so is that you don't need to worry about the single quotes.-- daver True. However, I'd like to know why you are ha...

Type Initializer for "System.Net.IpAddress" threw an exception
Apologies - I posted this in the GD forum by mistake instead of here... .... System: Windows XP Professional The other day I had a perfect working copy of Web Matrix on which I am developing an MS SQL based package. Then,  two things happened. The first is that my Outlook 2000 Premium was reconfigured to collect and send mail via an Exchange Server as well as my normal POP3 based system.  The second is that a Blue Tooth mouse was installed. Now,  whenever I load a source file into WebMatrix and press the 'F5' key to launch the web server I get this error: "The Type...

Type Initializer for "System.Net.IpAddress" threw an exception
.... System: Windows XP Professional The other day I had a perfect working copy of Web Matrix on which I am developing an MS SQL based package. Then,  two things happened. The first is that my Outlook 2000 Premium was reconfigured to collect and send mail via an Exchange Server as well as my normal POP3 based system.  The second is that a Blue Tooth mouse was installed. Now,  whenever I load a source file into WebMatrix and press the 'F5' key to launch the web server I get this error: "The Type Initializer for 'System.Net.IpAddress' threw an exception" and the following...

"cast from type 'dbnull' to type 'string' is not valid"
I keep getting the following error in my code: "cast from type 'dbnull' to type 'string' is not valid"My code is as follows:Do While DR.Read tbxRefNo.Text = Trim(DR("Ref")) tbxTitle.Text = Trim(DR("Title")) If Not IsDBNull(DR("Signed")) Then tbxDateSigned.Text = DR("Signed") Else tbxDateSigned.Text = "" End If tbxStatus.Text = DR("StatusName") tbxSignedBy.Text = DR("SignedBy") LoopI am reading the values from a database using the loop but I was getting a problem when I read a record that had a NULL value in the "Signed" field. I changed the code to put in the I Not IsDBNull...&nbs...

"An unhandled exception of type 'System.StackOverflowException' occurred in system.data.dll"
An error "An unhandled exception of type 'System.StackOverflowException' occurred in system.data.dll" is occuring when i debug my project.Initially it was working fine but from last 2 days its giving me this error.asset.dll is dll created by me which i m using here.this dll is already added in the reference.If any solution reply..:)  Hi, If possible then send the code where this exception is encountered.You can check for recursion in the code , try the link below too. http://support.microsoft.com/kb/317161 can you give more details or screen shotAmit RegardsAmit Hi:  ...

[ID 20010330.006] incorrect WARNING from installperl "You've never run 'make test' or some tests failed!"
This is a bug report for perl from dcd@tc.fluke.com, generated with the help of perlbug 1.33 running under perl v5.7.0. ----------------------------------------------------------------- [Please enter your report here] After doing a make test in the the latest perl 5.7.0 it reported "All tests successful." so I was surprised that make install starts off by issuing the WARNING that I had "never run 'make test' or some tests failed!" dd:perl-current$ sudo make install ../perl installperl WARNING: You've never run 'make test'...

Message="Conversion from type 'DBNull' to type 'Integer' is not valid."
I have this code in Dardanelle.aspx page. 12. Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) 13. Dim pages As Integer14. pages = CInt(e.Command.Parameters("@pageCount").Value) 15. Pn2.Count = pages 16. End Sub The error below refers to line 14 above System.InvalidCastException was unhandled by user code Message="Conversion from type 'DBNull' to type 'Integer' is not valid." Source="Microsoft.VisualBasic" StackTrace: at Microsoft.VisualBasic.CompilerServ...

System.TypeInitializationException: The type initializer for "Test.Form.DataProvider" threw an exception. --->
 Hi I am getting an exception  whenever I add a new function  or sub procedure  to   Data provider class in  a module development.  I am developing  a form module  its name as  Test.From.   The  exception  report follows   DotNetNuke.Services.Exceptions.ModuleLoadException: The type initializer for "Test.Form.DataProvider" threw an exception. ---> System.TypeInitializationException: The type initializer for "Test.Form.DataProvider" threw an exception. ---> System.ArgumentNullException: Value cannot be nul...

Exception {"Conversion failed when converting the nvarchar value 'Chuck Designs' to data type int."}
I have the below methods. They generate the error above at the sqlreader.read(); line. Please help.  All I want is a simple true false check to see if a value is already in the db.public static void InsertVendor(string vendor) {if (!CheckVendorValue(vendor) == true) {using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["VendorEvaluationConnectionString"].ConnectionString)) { conn.Open();SqlCommand cmd = new SqlCommand("Insert into tblVendors values (@vendor_name)", conn); cmd.Parameters.AddWithValue("@vendor_name", ve...

run('explorer.exe "' + ls_wmv_file_name + '"')
Hi all, I have an PB 9 Application that run some WMV files I'm using the following command : run('explorer.exe "' + ls_wmv_file_name + '"') The problem is that it's working on some machines and not working on others What I realized, is that , when the command "explorer c:\file_name.wmv" is working from the run command of windows , then it works from my application. But on some machines, this command does not work from the run command of windows , and so it won't work form my application. So please, does anyone has any idea about ...

What is "A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.VisualBasic.dll"??
Hi,   I got this error message in my output window when debugging (but not everytime). In that page i send a xml request to server, waits for the response, then store the response to the database, when everything is done, i Response.Redirect("HotelSearchResult.aspx", False), if this error occurs, the data will not be shown in the HotelSearchResult.aspx. Is it something to do with the "false"? i got another first chance exception thread error so i add the "false" in the response.redirect statement. Can anyone help?? Jin Hiya,  Take a look here...

What is "A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.VisualBasic.dll"??
Hi,  I have a aspx page sends a xml request to server, waits for the response and then save data to the database. another aspx page is to display these data from the database. I got this error in my output window during debugging (it's strange becauses it does not appear for all the times, if it does occur, the second page is not displaying any data). Is it something to do with Response.Redirect("HotelSearchResult.aspx", False) ? i got the "A first chance exception of type System Threading ThreadAbortException occurred in mscorlib" error, so i add the "Fals...

Web resources about - Unit test causes exception "The type initializer for 'XXX' threw an exception" when running code which accesses appSettings of the webconfig - asp.net.config-deploy

Resources last updated: 1/19/2016 12:54:33 PM