0

The simplest way for me to describe this is in the Unit Testing context.

I have a method that accepts a parameter and asserts that parameter isn't null.

public void DoStuff(string requirementName)
{ 
    if (requirementName == null) throw new ArgumentNullException(nameof(requirementName)); 
}

Then I have a test that asserts this exception is thrown.

[Test]
public void NullRequirementName_ThrowsArgNullException()
{
    var sub = new DoStuffClass();
    Action doStuff = () => sub.DoStuff(null);
    doStuff.Should().Throw<ArgumentNullException>(); 
}

What I want is to assert that the message of the exception contains the name of the parameter, and yet allow the parameter name in the signature of DoStuff to change and give compile time indication that the name is changed.

[Test]
public void NullRequirementName_ThrowsArgNullException()
{
    var sub = new DoStuffClass();
    Action doStuff = () => sub.DoStuff(null);
    doStuff.Should().Throw<ArgumentNullException>().Where(ex => ex.Message.Contains("requirementName"); 
}

The above assertion works fine, but doesn't allow the param name to change without breaking the test at run-time. I'm looking for a simple looking syntax that effectively gives me nameof(DoStuffClass.DoStuff.requirementName)

Josh Gust
  • 4,102
  • 25
  • 41
  • 2
    Does this answer your question? [How can you get the names of method parameters?](https://stackoverflow.com/questions/214086/how-can-you-get-the-names-of-method-parameters) – devNull Jan 23 '20 at 18:16
  • Unit tests are supposed to break when there is a code change. If the unit test is about comparing the parameters name then it should break when the names of parameters change. If the unit tests pass even when the code changes, you will never know what impact the code change will bring in the actual application flow. – Chetan Jan 23 '20 at 18:34
  • @BryanLewis It seems like it may be a dup to that, is the answer still "no"? – Josh Gust Jan 23 '20 at 18:34
  • @ChetanRanpariya thanks, I do understand the premise of unit testing. I want compile time indication for something as trivial as the name of a parameter. The actual test is that the parameter name appears in the exception message, not that the parameter name is a particular value. – Josh Gust Jan 23 '20 at 18:36
  • *"Unit tests are supposed to break when there is a code change."* I don't think that's a rule at all. They only break if there's a *breaking* code change... – Rufus L Jan 23 '20 at 18:36
  • According to the duplicate, you can use reflection to get the parameter name. Then you can use that value to compare with the exception message returned from the method. So the answer is "yes". – Rufus L Jan 23 '20 at 18:37
  • @RufusL yeah, I was hoping to not use reflection, but I think I'm resigning to the inevitable here. – Josh Gust Jan 23 '20 at 18:38
  • The change is breaking or not that will be decided if the the code is covered by unit testing. – Chetan Jan 23 '20 at 18:38
  • @JoshGust, it appears that Rufus beat me to answering your comment. Essentially, the answer is "no" if you are looking for something as straight-forward as a "nameof" equivalent. – Bryan Lewis Jan 23 '20 at 18:50
  • `var name = typeof(Namespace.ClassName).GetMethod("MyMethod").GetParameters()[0].Name;` – Rufus L Jan 23 '20 at 20:07

0 Answers0