简体   繁体   English

单元测试由于未初始化的静态属性而失败

[英]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. 单元测试中的属性值始终为null,因为未初始化源类。 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: 静态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: 静态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? 在静态类B中调用方法之前,是否可以强制单元测试代码初始化静态类A?

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: 为了直接回答您的问题,可以在调用静态类B中的方法之前强制单元测试代码初始化静态类A-为此,您只需要访问类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. 但是,这可能不是代码的问题,因为您正在访问类B中的App.AppDir (如果您接受了对问题的编辑,最初只是对AppDir ),则应正确初始化它。

The variable appDir of your class A is initialized in static void Main() , which doesn't run on Unit Testing. 您的类A的变量appDir是在static void Main()初始化的,该变量不会在单元测试中运行。 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. 只需更改您的类App即可绕过静态字段,然后将目录放入静态属性getter中。 The field appDir most likely isn't set yet, because Main() isn't called before the static field initializer on idbCn is called. 很有可能尚未设置字段appDir ,因为在idbCn上的静态字段初始化程序之前未调用Main()

static class App {

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

  [STAThread]
  static void Main() {

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

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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