I want to unit test my rendering engine for an ASP.NET app. I have some custom controls created in that app. Most of those control rely on having CreateChildControls() method called by the ASP.Net engine wheather during initial call or a postback.
When running a test CreateChildControls() does not get called and the control is in an “invalid” state.
As a workaround to get the tests going I explicitly call some other public method that calls EnsureChildControls() in it. This in effect causes CreateChildControls() to be executed and the control being initialized properly (just as it would normally when hosted on web server).
I would like to get rid of that extra method call and the control to still be in a proper state for testing.
Are there any elegant solutions to this problem?
You could alternatively use reflection to call CreateChildControls directly, thereby escaping the need to use the public method to invoke it. That would be easy enough:
You could embed this code somewhere so that you only need to do this once and reuse it.
EDIT: You could have your test inherit from your control too, so you can invoke some of these methods directly. But you have to instantiate your control by instantiating the test and not the control, as in:
HTH.