How to read Web.Config file while Unit Test Case Debugging?
Asked Answered
R

3

4

I am trying to fetch data(URL) from my configuration file

i.e :

<AppSettings>
<add key="configurationUrl" value="http://xx.xx.xx.xx:XXXX/configuration service/configurations"/>

using the following code

 reqClient.BaseAddress = System.Configuration.ConfigurationManager.AppSettings["configurationUrl"].ToString();

Its all working good when I am trying to debug it, but the major problem comes when I debug a unit test case calling the same code above, instead of Showing "configurationUrl" in the AllKeys of APPSettings its showing "TestProjectRetargetTo35Allowed". I have also added the web.config file in the testcase project.

Any assistance will be appreciated Thank You.

Robichaux answered 7/5, 2014 at 9:3 Comment(4)
Maybe you are looking for this MSDNNiemi
A configuration file is a real file that exists in the real file system, therefore what you are doing is not a unit test, it's an integration test.Cochin
I want to test a function of me which is using configuration file but over all I am just taking up a single Unit.Robichaux
Can you post the code that you are trying to test? It may make more sense to pass the values in as parameters rather than making the unit test and the code being tested dependent upon your configuration file.Maddeu
N
13

I suggest you create some abstraction layer to get rid of the dependency to ConfigurationManager. For example:

public interface IConfigurationReader
{
    string GetAppSetting(string key);    
}

public class ConfigurationReader : IConfigurationReader
{
    public string GetAppSetting(string key)
    {
        return ConfigurationManager.AppSettings[key].ToString();
    }
}

Then you could mock this interface in your unit test.

Niemi answered 7/5, 2014 at 9:9 Comment(1)
Sorry, not the way out anything else I have to read from Config file can you please tell why this error is coming whats the issueRobichaux
F
5

An easy fix would be to simply copy the web.config over from the .web project and paste into your test project, renaming it to app.config in the test project.

This is very bad though as unit tests should not rely on external dependencies, but a fix none the less!

@Jon_Lindeheim has the cleanest method however by abstracting to interface so that you can stub the return value in your test!

Footie answered 15/1, 2016 at 14:38 Comment(2)
This is what I ended up doing, not the best but quick.Whisler
renaming to app.config solved my problem. thanks.Hearthside
C
4

The issue is that you haven't created an app.config file for your test project. You've created a web.config file, but that's getting completely ignored since the test project isn't a web project.

That being said, @Jon_Lindeheim is absolutely right about introducing an abstraction layer on top of the ConfigurationManager. (The ConfigurationManager is an external dependency, which means your unit test is testing more than just the SUT.)

Clearcut answered 7/5, 2014 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.