James Maroney
James Maroney

Reputation: 3256

Testing un-compilable code in NUnit

I have a class which for now should always have a certain member populated before it is valid. To enforce this, the class has no default constructor and instead has a constructor which accepts a value for that required member. The setup is similar to this below:

public class MyClass
{
  public string Owner { get; protected set; }

  public MyClass(string owner)
  {
    this.Owner = owner;
  }
}

Now I'd like to write a test to ensure that there is in fact no default constructor, so that if one gets added in the future, we are reminded of the reasons behind not having one and are forced to consider the impact of doing so. Although, obviously attempting to call the default constructor in a test won't just fail, it won't compile.

Is there a good way to pull off this kind of test without modifying my original class? If not, I suppose I could implement a default constructor which throws an exception. My only hesitation there is that calling the default constructor now becomes compilable code and then we must rely on other tests to ensure such code doesn't get written.

Thoughts?

Upvotes: 4

Views: 289

Answers (6)

Nick W
Nick W

Reputation: 155

Yep. A good way would be to use reflection to try a parameterless constructor within a try/catch.

Upvotes: 0

plinth
plinth

Reputation: 49199

ConstructorInfo ci = typeof(MyClass).GetConstructor(Type.EmptyTypes);
Assert.IsNull(ci);

Upvotes: 5

No Refunds No Returns
No Refunds No Returns

Reputation: 8346

I would create a default constructor, mark it private and put your documentation there. Then your reasons for doing it won't be hidden off somewhere. You have to realize you'll be giving up some serialization functionality that requires the parameterless constructor.

Upvotes: 9

Sam Holder
Sam Holder

Reputation: 32946

you could use reflection to check if there is a no arg constructor for the class and fail the test if there is

Upvotes: 0

womp
womp

Reputation: 116987

You could call Activator.CreateInstance(typeof(MyClass)) to try to run the default constructor, and assert that a MissingMethodException is thrown.

[Test]
[ExpectedException(typeof(MissingMethodException))
public void ShouldBeNoDefaultConstructorForMyClass()
{
    Activator.CreateInstance(typeof(MyClass));
}

Upvotes: 10

Related Questions