C# 如何使用包装类或依赖项注入为静态方法和静态变量编写测试用例

C# 如何使用包装类或依赖项注入为静态方法和静态变量编写测试用例,c#,unit-testing,xunit,C#,Unit Testing,Xunit,我的需求是需要使用xunit为Example1视图模型编写测试用例。此视图模型初始化示例2视图模型。但是Example2在构造函数中包含静态方法,并且该静态方法包含一个静态变量 如果我为Example1编写了一个测试用例,那么测试用例在运行所有测试用例时失败,但在运行所选测试用例时通过。因为在示例2中使用了静态方法 我尝试过将静态方法和变量更改为非静态,但它抛出System.TypeInitializationExceptionexception 有人能解释或举例说明这一点吗?在不删除静态关键字

我的需求是需要使用xunit为
Example1
视图模型编写测试用例。此视图模型初始化
示例2
视图模型。但是
Example2
在构造函数中包含静态方法,并且该静态方法包含一个静态变量

如果我为
Example1
编写了一个测试用例,那么测试用例在运行所有测试用例时失败,但在运行所选测试用例时通过。因为在
示例2中使用了静态方法

我尝试过将静态方法和变量更改为非静态,但它抛出
System.TypeInitializationException
exception

有人能解释或举例说明这一点吗?在不删除静态关键字的情况下,我如何实现这一点?谁能给我一个指导吗

例如:

public class Example1
{
    public Example1(Example2 example2) { ... }
}   

public class Example2
{
    public Example2()
    {
        SomeStaticMethod() //static method inside the constructor
    }

    static SomeStaticMethod()
    {
        logPath = ""; //logPath is the static variable which is declared in another static class
    }
}

如果您只需要在构造函数中传递
Example2
的实例,它相当简单:

namespace Tests
{
  using Xunit;

  using XunitSample;

  public class Class1
  {
    [Fact]
    public void Example1_Test()
    {
      var ex2 = (Example2)System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(Example2));

      var target = new Example1(ex2);

      Assert.NotNull(target);
    }
  }
}

你们有什么版本的visual studio?为什么它必须是
xunit