3

I have a custom configuration section, something like:

<fooSection>
    <foo>
      <add bar="cow" />
      <add bar="dog" />
      <add bar="goat" />
    </foo>
</fooSection>

I'm trying to figure out how I can add/edit this section in the app.config file in a test fixture setup. I don't really know where to start, but an obvious problem is that my ConfigurationSection does not expose any setters.

fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253

2 Answers2

0

As far as I know, you can't do it through the normal code, because, as you mentioned, it only exposes getters. You can change the file however, as it is just another text file. But then the changes will probably not be picked up without restarting the application (in your case your test runner).

So you might want to look at another mechanism for this. For example a small database, an XML file, application settings, ...

Peter
  • 13,733
  • 11
  • 75
  • 122
0

You should be able to use DeserializeSection - just pass it an XmlReader that points to the configuration.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • I don't follow you. I'm trying to write to the config file. Can you give an example? – fearofawhackplanet Dec 12 '11 at 12:20
  • @fearofawhackplanet - Why are you trying to write to the config file in your tests? – Oded Dec 12 '11 at 12:22
  • The config file contains scheduling information which needs to be set relative to the current date in order that the test results are consistent. – fearofawhackplanet Dec 12 '11 at 12:44
  • @fearofawhackplanet - So your code depends on static configuration? – Oded Dec 12 '11 at 12:46
  • well, it depends on a config file yes, if that's what you mean. – fearofawhackplanet Dec 12 '11 at 12:50
  • Looks like bad design... http://www.devtrends.co.uk/blog/configuration-settings-are-a-dependency-that-should-be-injected – Oded Dec 12 '11 at 12:51
  • I agree completely with that article, and I have unit tests in place already. I am now trying to write a simple end to end/integration test and imho that should include executing the schedule as specified in the config file, since that is part of the specification. – fearofawhackplanet Dec 12 '11 at 12:58
  • @fearofawhackplanet - OK. My answer was that you should be able to deserialize XML into an instance of your `ConfigurationSection` class and pass that into your code. – Oded Dec 12 '11 at 13:03