C# 为 nUnit 测试创建委托静态方法的模拟

发布于 2025-01-12 21:58:16 字数 2206 浏览 1 评论 0原文

我正在研究 nUnit。我需要从主题下的同一类中设置静态方法的模拟。我想知道我是否正确创建了模拟,因为我定义了两个实例;被测试类的 sut 和同一类方法的模拟,以 MockObject.Setup(..) 为目标,它们存在于我试图测试的同一个类中。 GetNumbers() 方法的模拟

我需要帮助创建 ProcessCalculation 类 error

在此处输入图像描述

public class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Testing Automation Proptype... ");

        ProcessCalculation process = new ProcessCalculation();

        process.Run();

        Console.ReadLine();
    }
}

计算

public class ProcessCalculation
{
    public delegate List<int> GetNumbersPointer();

    //GetNumbersPointer getNumbersPointer = new GetNumbersPointer(GetNumbers);
  
    public GetNumbersPointer getNumbersPointer = GetNumbers;

    public void Run()
    {
        var Nos = getNumbersPointer.Invoke();

        int x = Nos[0];
        int y = Nos[1];

        var z = Add(x, y);

        Console.WriteLine("sum  "+ z);
    }

    public static int Add(int x, int y)
    {
        return x + y;
    }

    public static List<int> GetNumbers()
    {
        List<int> myNo = new List<int>();

        myNo.Add(32);
        myNo.Add(10);

        return myNo;
    }
}

测试类

 public class Tests
{
    private readonly ProcessCalculation sut;
    private readonly Mock<ProcessCalculation> processCalculationMoq;
   

    public Tests()
    {
        sut = new ProcessCalculation();
        processCalculationMoq = new Mock<ProcessCalculation>();
    }


    [SetUp]
    public void Setup()
    {
    }

    [Test]
    public void Test1()
    {
        //Arrange
        var fixture = new Fixture();

        var getNumberMoq = fixture.CreateMany<int>(2);

        processCalculationMoq.Setup(x => x.getNumbersPointer).Returns(getNumberMoq); // this throw error.. I want to mock using setup
        
        //???????????? how use delegate to mock static GetNumbers() method  
      

        //Assert
    }
}

I am working on nUnit. I need to Setup mock of a static method from same class that is under subject. I am wonder If I created mock correctly as I have defined two instances; sut of class under test and mock of same class method in objective to MockObject.Setup(..) that exist in the same class that I am trying to test. I need help to create mock of GetNumbers() method that is in ProcessCalculation class

error

enter image description here
main

public class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Testing Automation Proptype... ");

        ProcessCalculation process = new ProcessCalculation();

        process.Run();

        Console.ReadLine();
    }
}

Calculation

public class ProcessCalculation
{
    public delegate List<int> GetNumbersPointer();

    //GetNumbersPointer getNumbersPointer = new GetNumbersPointer(GetNumbers);
  
    public GetNumbersPointer getNumbersPointer = GetNumbers;

    public void Run()
    {
        var Nos = getNumbersPointer.Invoke();

        int x = Nos[0];
        int y = Nos[1];

        var z = Add(x, y);

        Console.WriteLine("sum  "+ z);
    }

    public static int Add(int x, int y)
    {
        return x + y;
    }

    public static List<int> GetNumbers()
    {
        List<int> myNo = new List<int>();

        myNo.Add(32);
        myNo.Add(10);

        return myNo;
    }
}

Test Class

 public class Tests
{
    private readonly ProcessCalculation sut;
    private readonly Mock<ProcessCalculation> processCalculationMoq;
   

    public Tests()
    {
        sut = new ProcessCalculation();
        processCalculationMoq = new Mock<ProcessCalculation>();
    }


    [SetUp]
    public void Setup()
    {
    }

    [Test]
    public void Test1()
    {
        //Arrange
        var fixture = new Fixture();

        var getNumberMoq = fixture.CreateMany<int>(2);

        processCalculationMoq.Setup(x => x.getNumbersPointer).Returns(getNumberMoq); // this throw error.. I want to mock using setup
        
        //???????????? how use delegate to mock static GetNumbers() method  
      

        //Assert
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱ヾ落雨殇 2025-01-19 21:58:16

将您的实现更改为

public class ProcessCalculation
{
    public delegate List<int> GetNumbersPointer();

    // If InternalsVisibleTo(Unit Test assembly) is specified, this can be internal
    // Also, "new DelegateType(Target)" is not needed, the compiler inserts
    // that implicitly
    public GetNumbersPointer getNumbersPointer = GetNumbers;

    // ...
}

然后,在您的测试中:

public class Tests
{
    private readonly ProcessCalculation sut;
   

    public Tests()
    {
        sut = new ProcessCalculation();
        sut.getNumbersPointer = TestGetNumbers;
    }

    [Test]
    public void Test1()
    {
        sut.Run(); 
    }

    public static List<int> TestGetNumbers()
    {
        // Return test data
        return new List<int> { 0, 1, 2, 3...};
    }
}

Change your implementation to

public class ProcessCalculation
{
    public delegate List<int> GetNumbersPointer();

    // If InternalsVisibleTo(Unit Test assembly) is specified, this can be internal
    // Also, "new DelegateType(Target)" is not needed, the compiler inserts
    // that implicitly
    public GetNumbersPointer getNumbersPointer = GetNumbers;

    // ...
}

And then, in your test:

public class Tests
{
    private readonly ProcessCalculation sut;
   

    public Tests()
    {
        sut = new ProcessCalculation();
        sut.getNumbersPointer = TestGetNumbers;
    }

    [Test]
    public void Test1()
    {
        sut.Run(); 
    }

    public static List<int> TestGetNumbers()
    {
        // Return test data
        return new List<int> { 0, 1, 2, 3...};
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文