简体   繁体   中英

Unit Test Fails due to uninitialized static property

I have a static method in a static class that fails under unit testing. The problem is that it requires the value of a public static property from a different static class. The value of the property in the unit test is always null because the source class is not initialized. Both static classes are in the same project. If I execute the method directly, I get the desired results, but I am unable to use a test.

Static Class A:

static class App {

  private static string appDir;

  public static string AppDir => appDir;

  [STAThread]
  static void Main() {

    appDir = AppDomain.CurrentDomain.BaseDirectory;

    DbEng.PutIdbVal("x", "Y");  // Method under test - Works here

  }
}

Static Class B:

public static class DbEng {

  private static SQLiteConnection idbCn = new SQLiteConnection("Data Source=" + App.AppDir + "gcg.idb"); // App.AppDir is valid when not testing, is null when testing.

  public static void PutIdbVal(string key, string value) {

  using (var cmd = new SQLiteCommand("INSERT INTO tKv (Key, Value) VALUES (@key, @value)", idbCn)) {
      cmd.Parameters.Add(new SQLiteParameter("@key", key));
      cmd.Parameters.Add(new SQLiteParameter("@value", value));
      idbCn.Open();
      cmd.ExecuteNonQuery();
      idbCn.Close();
    }
  }
}

Unit Test:

[TestClass]
public class DbEng_Tests {
  [TestMethod]
  public void PutIdbVal_Test() {

    string TestKey = "Test-Key";
    string TestValue = "Test - Value";

    DbEng.PutIdbVal(TestKey, TestValue);

  }
}

Is it possible to force the unit testing code to initialize static class A before calling the method in static class B?

Static classes are initialized upon first access prior to first use of any static member of that class. Unit Test code is no exception to this rule.

To answer your question directly, it is possible to force the unit testing code to initialize static class A before calling the method in static class B - to do so, you just need to access any public static member of class A:

string appDir = App.AppDir;

However, this may not be what's wrong with your code, as you're accessing App.AppDir (if you accepted my edit of your question, which originally just had AppDir ) in class B, which should initialize it properly.

The variable appDir of your class A is initialized in static void Main() , which doesn't run on Unit Testing. You should add a static constructor instead:

static App()
{
     appDir = AppDomain.CurrentDomain.BaseDirectory;
}

Just change your class App to bypass the static field and put the directory in the static property getter. The field appDir most likely isn't set yet, because Main() isn't called before the static field initializer on idbCn is called.

static class App {

  public static string AppDir => AppDomain.CurrentDomain.BaseDirectory;

  [STAThread]
  static void Main() {

    DbEng.PutIdbVal("x", "Y");  // Method under test - Works here

  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM