3

The way I have my unit test set up is that a user can run the executable and select a dataset to use. This information is stored in app.config, however when I try to access app.config when running the unit test with Gallio Icarus it doesn't work.

Edit: I got the field to bind to an Application property however when I call Properties.Settings.Default.Save(); it doesn't seem to do anything. The app.exe.config does not change and the changes does not persist.

Reflux
  • 2,929
  • 4
  • 26
  • 27
  • How doesn't it work? I ask because I am using Gallio and MbUnit and I can include and access an app.config file with out a problem – Matt Ellen Aug 04 '10 at 12:29
  • I use Properties.Settings.Default. When I use that in the unit test the values are empty. – Reflux Aug 04 '10 at 13:29
  • I'm not familiar with that - can you show us the code you're using? If you need to get at the `appSettings` you can use the `AppSettingsReader` object. – Matt Ellen Aug 04 '10 at 13:38
  • Actually now the app.config doesn't seem to save. I have a field for username and added a property binding for the text. To save the setting I call Properties.Settings.Default.Save() however if I look at the config file the value for the Username is blank but if I launch the application again I see the username in the field. – Reflux Aug 04 '10 at 14:46

2 Answers2

3

Mock how you read the app config.

In your app write an interface & class that gets data from the app.config file. In your test, implement the interface on a dummy object, that will pass back known outputs each time.

The class that you're testing needs to take a parameter (on ctor or other) that is an object that implements the interface.

In your real code this will be the real object that reads from the app.config.

In the tests it will be the dummy object.

Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
2

app.config is the file that corresponds to the MyProgramme.exe.config file that the programme reads on start up. This can be accessed via the Configuration object in the System.Configuration namespace and assembly.

Properties.Settings.Default corresponds to the file user.config that your programme will generate when you call Properties.Settings.Default.Save()

So including the app.config file in the tests won't affect Properties.Settings.Default.

For more information see Using Settings

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
  • I tried it with user.config however when I run my unit test it doesn't read from the user.config that was created when I ran my application. Is there a way to have it read the user.config or should I be using app.config? – Reflux Aug 05 '10 at 12:33