is supplying a default constructor considered a best practice for testing?
I'm working with the Moles Framework for mocking. I'm running into so开发者_JAVA技巧me roadblocks with objects that don't have argument-less constructors. When I create the object in question, I have to provide an ID, which kicks off a call to the database to set the objects properties. Which means a lot more interception via Moles. If the object had a simple constructor my testing would go a lot smoother.
I remember hearing somewhere that providing a default constructor is important for testing. When I went to google to confirm my recollection, I couldn't find an answer.
Providing an argument-less constructor: helpful or important for testing? Irrelevant?
I'm of the opinion that you shouldn't really be doing that sort of thing in your object constructor anyway, since it ties your tests too tightly to your database to be of any use. A dependency injection framework like Ninject would help massively here. You don't have a default constructor but you do a little setup and let Ninject glue together everything that's needed when you create your test objects.
I'm only starting out with Ninject (and using Moq) so I haven't had a massive amount of experience with it but it removes the need for default constructors used for testing. It also sorts out a bit more and helps to make your tests totally independent of anything external.
I know it's a language-independent question so Ninject might be irrelevant for you but it's my general view on unit testing.
精彩评论