Get values from the web.config section in an app.config file?

I guess I'm confused here; it looks like you're trying to test that ASP. NET is using your custom membership provider appropriately. Correct?

Up vote 3 down vote favorite share g+ share fb share tw.

I'm trying to unit test values that will eventually wind up in a web. Config file. In my test project, I created an app.

Config file with a web. Config section to hold the settings. In a normal situation, I would call System.Configuration.

ConfigurationSettings. AppSettings, but in this case, that doesn't work. I saw this question, which is very similar, but doesn't address how to get the NameValueCollection out of the config file.

Here is an example of the config file: Has anyone dealt with this before? C# .net asp.net web-config app-config link|improve this question asked Mar 4 '09 at 21:01Mark Struzinski5,079104675 90% accept rate.

I guess I'm confused here; it looks like you're trying to test that ASP. NET is using your custom membership provider appropriately. Correct?

If so, I'm 99.999% sure that you cannot unit test this using the MS framework; you must integration test it by deploying it to the webserver (or running Cassini in VS) and typing a username/password into your login page. Now, it's possible I've misunderstood your request. If so, let me know and I'll edit my answer accordingly.

Edit: For right now, I'm really just trying to test the NameValue pairs coming out of the config file, to make sure that if the values aren't present, my defaults are being applied. In other words, I want to try to pull applicationName, and verify that it equals "SettlementInfo", and so on. After that, I will be using integration testing to ensure that ASP.

NET is using the custom framework in place of the default one. Does that make sense? I need more than a comment to reply, so I'm editing.

If I read you correctly, you are wanting to unit test your program to ensure that it deals with configuration correctly, yes? Meaning you want to ensure that your code grabs, for example, the correct AppSettings key and handles a null value therein, correct? If that's the case, you're in luck; you don't need an app.

Config or web. Config at all, you can set the values you need as part of your test setup. For example: TestMethod public void Test_Configuration_Used_Correctly() { ConfigurationManager.

AppSettings"MyConfigName" = "MyConfigValue"; MyClass testObject = new MyClass(); testObject. ConfigurationHandler(); Assert. AreEqual(testObject.

ConfigurationItemOrDefault, "MyConfigValue"); } TestMethod public void Test_Configuration_Defaults_Used_Correctly() { // you don't need to set AppSettings for a non-existent value... // ConfigurationManager. AppSettings"MyConfigName" = "MyConfigValue"; MyClass testObject = new MyClass(); testObject. ConfigurationHandler(); Assert.

AreEqual(testObject. ConfigurationItemOrDefault, "MyConfigDefaultValue"); }.

For right now, I'm really just trying to test the NameValue pairs coming out of the config file, to make sure that if the values aren't present, my defaults are being applied. In other words, I want to try to pull applicationName, and verify that it equals "SettlementInfo", and so on. – Mark Struzinski Mar 4 '09 at 21:34 After that, I will be using integration testing to ensure that ASP.

NET is using the custom framework in place of the default one. Does that make sense? – Mark Struzinski Mar 4 '09 at 21:35 See my edit for an answer.

I may still not understand where you're going with this, but if it's what I think it is, this should start you in the right direction. – Randolpho Mar 4 '09 at 22:30 This did it. I just needed to think about the problem in a different way.

Thanks for the help! – Mark Struzinski Mar 5 '09 at 14:41 Glad that worked. :) – Randolpho Mar 5 '09 at 15:05.

You should be able to use the ConfigurationManager.GetSection() method to pull out whatever you want.

I've tried ConfigurationManager.GetSection() on the system. Web and membership sections. Both return null.

Am I doing something wrong? – Mark Struzinski Mar 4 '09 at 21:57.

I believe you only have access to the webconfig file while your application is actually beeing started up. The solution is rather easy -> "Fake" your config. Use a NameValueCollection and use that instead: private static NameValueCollection CreateConfig() { NameValueCollection config = new NameValueCollection(); config.

Add("applicationName", "TestApp"); config. Add("enablePasswordReset", "false"); config. Add("enablePasswordRetrieval", "true"); config.

Add("maxInvalidPasswordAttempts", "5"); config. Add("minRequiredNonalphanumericCharacters", "2"); config. Add("minRequiredPasswordLength", "6"); config.

Add("requiresQuestionAndAnswer", "true"); config. Add("requiresUniqueEmail", "true"); config. Add("passwordAttemptWindow", "10"); return config; } Now you could easily pass that collection into your class that parses data from it.

Actually, if you are using NUnit, you can stick that in an App. Config in your test project. Then add this line to your Post-build event: copy /Y “$(ProjectDir)App.

Config” “$(TargetDir)$(TargetFileName). Config” When you create the new provider in your tests, NUnit will pass the values in your app. Config to the provider in the initialize method.

Why not just stick it in the web. Config file?

I'm in a class library for unit testing, so I'm using the app.config. Once I get to adding it to my web project, it'll go in a web.config. – Mark Struzinski Mar 4 '09 at 21:06 Is there a way to reference a web.

Config file from a class library? – Mark Struzinski Mar 4 '09 at 21:07.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions