0

I have a factory class that has a private method like this:

[Conditional("DEBUG")]
private static void IsDebugCheck(ref bool isDebug)
{
    isDebug = true;
}

The logic is used later in the class like so to generate different implementations of the same interface, like this:

if (IsDebugCheck){
  return new ImplementationA();
}
else{
  return new ImplementationB();
}

I am trying to write unit tests for this now, and want to somehow set the conditional value within the test. I'm using MsTest for this and have been googlign and found no good information on how to do this.

[TestMethod()]
[TestCategory("Debug")]
public void FactoryShouldGenerateImplementationAIfInDebug()

[TestMethod()]
[TestCategory("Debug")]
public void FactoryShouldGenerateImplementationBIfInReleaseMode()

But so far have been utterly striking out on how to do this.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
  • Can you [edit] your question to include code demonstrating how the application is calling `IsDebugCheck`? Is the `Conditional` attribute a .NET framework attribute, or something your team built? – Greg Burghardt Jun 08 '23 at 15:15
  • Is DEBUG a preprocessor directive? I.e. is it just the difference between a debug build and a release build? If so, you can use the Conditional preprocessor directives like `#if DEBUG`. HOWEVER, having differences between debug and release is a bit smelly: testing debug code doesn't make much sense. You need to test what ends up in production. – JHBonarius Jun 08 '23 at 15:21

0 Answers0