Accessing web.config during unit tests

Discussion in 'ASP .Net' started by ChrisC, Jan 27, 2004.

  1. ChrisC

    ChrisC Guest

    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 =---
     
    ChrisC, Jan 27, 2004
    #1
    1. Advertising

  2. 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" <-spam.invalid> wrote in message
    news:4016b7fe$1_2@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
    > =---
     
    Chris Jackson, Jan 27, 2004
    #2
    1. Advertising

  3. ChrisC

    ChrisC Guest

    it is like that

    > 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 =---
     
    ChrisC, Jan 28, 2004
    #3
  4. Re: it is like that

    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" <-spam.invalid> wrote in message
    news:4017881e$1_2@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
    > =---
     
    Chris Jackson, Jan 28, 2004
    #4
  5. ChrisC

    ChrisC Guest

    Accessing web.config

    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 =---
     
    ChrisC, Jan 29, 2004
    #5
  6. ChrisC

    rochnet

    Joined:
    Nov 1, 2009
    Messages:
    1
    i had same problem. So i decided to use a resource file instead of setting key data in the web.config file.

    i moved all of my web.config data into a resource file, changed my code to lookup key data from the resource file instead of the web.config settings and was able to use csunit just fine.
     
    rochnet, Nov 1, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    3
    Views:
    766
    Guest
    Feb 5, 2004
  2. Replies:
    8
    Views:
    896
  3. CSharpner
    Replies:
    0
    Views:
    1,061
    CSharpner
    Apr 9, 2007
  4. Mark Slater

    capturing stdout during unit tests

    Mark Slater, Aug 8, 2007, in forum: Ruby
    Replies:
    4
    Views:
    142
    Mark Slater
    Aug 8, 2007
  5. dayo
    Replies:
    11
    Views:
    353
    Ilya Zakharevich
    Dec 16, 2005
Loading...

Share This Page