I have a class that at the moment should always have a certain member filled up to its validity. To enforce this, the class does not have a default constructor and instead has a constructor that takes on a value for this required member. The setting is similar to the one below:
public class MyClass { public string Owner { get; protected set; } public MyClass(string owner) { this.Owner = owner; } }
Now I would like to write a test to make sure that there is actually no default constructor, so if it is added in the future, we are reminded of the reasons why it does not have one, and are forced to take into account the impact of this. Although, obviously, an attempt to call the default constructor in the test will not just fail, it will not compile.
Is there a good way to take this test off without changing my original class? If not, I assume that I can implement a default constructor that throws an exception. My only doubt is that calling the default constructor now becomes compiled code, and then we must rely on other tests to ensure that such code is not written.
Thoughts?
source share