views:

81

answers:

2

I have an object the references a bunch of Properties.Settings.Default... values and I need to stub these in the unit test for this object.

Unfortunately the type for the settings object is declared as internal and so I can't access it from the unit test project.

How can I stub up the return values for these properties? I'm using Rhino Mocks for mocking.

+4  A: 

In general, I wouldn't. Instead of your "inner" objects actually reading Properties.Settings.Default, have them declare a cconfigurable property (either at construction time, or via properties), and have another piece of code populate them.

That way, you can test everything but the default reading stuff in your unit tests, and you become less coupled to a way of reading the defaults, making it easier to switch the mechanism in the future.

kyoryu
yes, this way the object is more reusable by not depending on properties.settings
Benny
Also "less likely to break." By looking up the defaults, the object has two responsibilities - its actual job, and knowing where to find the settings. It would be a serious pain if you have 1000 classes all relying on properties.settings, and then decide you want that data to come from somewhere else!
kyoryu
+2  A: 

Also another tip that you may find useful to get around the internal problem, you can actually selectively make your internal code visible to your unit tests. In the code you are testing, open up your AssemblyInfo.cs and add something like the following:

#if UNITTEST
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("YourUnitTestAssembly")]
#endif

Then you just need the UNITTEST symbol defined in your build and your test assembly will be able to view all internal members.

donovan