473,322 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Accessing web.config during unit tests

I am a relative newbie to .NET - two weeks, using everyday. My problem
is this. I have my asp.net application where the web.config contains
the database connection string. Normally i access this fine with
Application[ "connectionString" ].

However, I am now trying to use a unit testing framework (csunit
1.9.2) and when I try and run the tests I have created it is saying
"The ConnectionString property has not been initialized". The piece
of code it is using runs fine if I access it by clicking a button
that triggers it in one of my aspx pages, but not when it is
initiated by the unit test class.

Potential troubles - the class I am running the method in is a
standalone utility class ie it doesnt have a aspx page associated.
Dont know if this matters or not....
public string getXmlCommandByName( string commandName )
{
SqlConnection conn;
using (conn = new SqlConnection(
ConfigurationSettings.AppSettings[ "connectionString"
] ) )
{
conn.Open();

string sqlString = "SELECT xml FROM PDSXML WHERE command =
" + commandName;
SqlCommand request = new SqlCommand( sqlString, conn );
SqlDataReader results =
request.ExecuteReader();

return results.GetValue( 0 ).ToString();
}
}

As you can see i have been trying using different ways of accessing
the web.config

Help!

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 18 '05 #1
4 4315
If you are storing values in web.config, you access them using
ConfigurationSettings.AppSettings, which will pull values from the
appSettings portion of the web.config.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Client
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

"ChrisC" <ch************@hotmail-dot-com.no-spam.invalid> wrote in message
news:40**********@127.0.0.1...
I am a relative newbie to .NET - two weeks, using everyday. My problem
is this. I have my asp.net application where the web.config contains
the database connection string. Normally i access this fine with
Application[ "connectionString" ].

However, I am now trying to use a unit testing framework (csunit
1.9.2) and when I try and run the tests I have created it is saying
"The ConnectionString property has not been initialized". The piece
of code it is using runs fine if I access it by clicking a button
that triggers it in one of my aspx pages, but not when it is
initiated by the unit test class.

Potential troubles - the class I am running the method in is a
standalone utility class ie it doesnt have a aspx page associated.
Dont know if this matters or not....
public string getXmlCommandByName( string commandName )
{
SqlConnection conn;
using (conn = new SqlConnection(
ConfigurationSettings.AppSettings[ "connectionString"
] ) )
{
conn.Open();

string sqlString = "SELECT xml FROM PDSXML WHERE command =
" + commandName;
SqlCommand request = new SqlCommand( sqlString, conn );
SqlDataReader results =
request.ExecuteReader();

return results.GetValue( 0 ).ToString();
}
}

As you can see i have been trying using different ways of accessing
the web.config

Help!

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet
News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000
Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---

Nov 18 '05 #2
> using (conn = new SqlConnection(
ConfigurationSettings.AppSettings[ "connectionString"
] ) )


Like that? :) (from the code i quoted)

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 18 '05 #3
My bad - I just noticed that in the top text, where you were looking at
Application.

If you are not actively inside of a web application, you won't find the
web.config. However, you can put an app.config in that directory and put
sections in there, which is what you would do for a WinForms app. I haven't
tried this exact scenario specifically, but that's why it isn't working, and
what you should be able to do in order to fix things up. Sorry I wasn't
reading closely enough before. :-S

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Client
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

"ChrisC" <ch************@hotmail-dot-com.no-spam.invalid> wrote in message
news:40**********@127.0.0.1...
using (conn = new SqlConnection(
ConfigurationSettings.AppSettings[ "connectionString"
] ) )


Like that? :) (from the code i quoted)

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet
News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000
Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---

Nov 18 '05 #4
No worries. It seems to be a bit of a pain this. Its a web application
and i dont want to create a effectively duplicate config file just
for the tests as that would be silly as it wouldnt be using the
actual config settings used by the application. I tried writing a
class to access the web.config directly but it wasnt having any of it
- in use by another process (i assume the web app keeps it constantly
open).

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Maxim Khesin | last post by:
I want to have a config file with my python proggie, satisfying the following requirements: 1) support key->(value, default) 2) simple and intuitive to read and edit 3) easyly readable into a...
6
by: Tom Verbeure | last post by:
Hello All, so I'm now convinced that unit testing is really the way to go and I want to add it to an existing project. The first thing that I find out, is that, well, it's hard work. Harder than...
14
by: | last post by:
Hi! I'm looking for unit-testing tools for .NET. Somthing like Java has --> http://www.junit.org regards, gicio
1
by: vivek sinha | last post by:
Hi, I am using the UnitTest Framework provided in Visual Studio 2005. I have created a seprate project/test assembly which contains all my unit tests for testing all the methods of another...
6
by: Primera | last post by:
I'm looking for the best way to test individual methods and such during development. I use VS2005, but am still new to .NET. I'd like to test things as I develop without executing the entire app...
72
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
6
by: Michael Bray | last post by:
I've just inherited a fairly large project with multiple classes. The developer also wrote a huge number of unit tests (using NUnit) to validate that the classes work correctly. However, I don't...
5
by: shuisheng | last post by:
Dear All, I was told that unit test is a powerful tool for progamming. If I am writing a GUI code, is it possible to still using unit test? I have a little experience in using unittest++. But...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.