简体   繁体   English

在另一个静态类C#中模拟方法

[英]Mock a method in another static class c#

I want to ask the best way how to mock static a method in an another class. 我想问一下如何在另一个类中模拟静态方法的最佳方法。 I know that mock is not working for a static class. 我知道模拟不适用于静态类。 Here is my Code so far. 到目前为止,这是我的代码。 I don't want to call SearchSomething() at the time because it's external interaction 我不想同时调用SearchSomething(),因为它是外部交互

public ResponseBase GetData(string searchId)
    {
        try
        {
            var request = new SearchRequest 
            {
                SearchId = searchId
            };
            var response = SearchLogic.SearchSomething(request);
            return response;
        }
        catch (Exception e)
        {
            return ResponseBase.ExceptionHandling(e);
        }
    }

public class SearchLogic(){
    public static ResponseBase SearchSomething(SearchRequest request)
    {
        //Do Something
        return  new ResponseBase;
    }
}

This is my UnitClass 这是我的UnitClass

[TestClass]
public class UnitClass
{

    [TestMethod]
    public void PositiveSearchTest()
    {
        //arrange
        string searchId = "name";
        var expected = new SearchRequest();
        SearchtController search = new SearchtController();

        var staticMock = new Mock<SearchLogic>();
        staticMock.Setup(s => s.SearchSomething()).Returns(new ResponseBase());

        //act
         var actual = search.GetData(searchId);

        //assert
         Assert.AreEqual(actual, expected);
    }
}

While this question gives one way to solve this, my preferred solution would be different: modify SearchLogic so it is no longer static. 尽管此问题提供了一种解决方案,但我的首选解决方案将有所不同:修改SearchLogic ,使其不再是静态的。 After that, you can then mock it to your heart's content. 之后,您可以根据自己的喜好嘲笑它。 Static methods are always a complete pain for unit testing; 静态方法始终是单元测试的全部难题。 I try to use them only for situations where there is one and only one correct behaviour. 我尝试仅在只有一种正确行为的情况下使用它们。

This obviously assumes you have the ability to modify SearchLogic . 显然,这假定您具有修改SearchLogic的能力。 If you don't, see the linked question. 如果不这样做,请参阅链接的问题。

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

相关问题 如何在C#中的静态类中模拟普通方法? - How to mock a normal method inside a static class in C#? 模拟静态C#数据库类 - mock static C# database class c#中的模拟文件IO静态类 - Mock File IO static class in c# 模拟静态方法Activator.CreateInstance以返回另一个类的模拟 - Mock static method Activator.CreateInstance to return a mock of another class 从 c# 中的另一个 class 调用非 static void 方法 - Calling a non static void method from another class in c# c# 控制台应用程序创建公共静态类并在此类中创建另一个静态方法 - c# console app crating public static class and in this class make another static method 如何在没有依赖注入的情况下模拟我们在 c# 中测试的相同 class 的另一种方法 - How to Mock another method of same class we are testing in c# without dependency injection C# 为 nUnit 测试创建委托 static 方法的模拟 - C# Create mock of delegate static method for nUnit test C#在另一个类的public方法中找不到静态方法 - C# cannot find static method inside public method of another class C#当另一个类中的静态方法被调用时,如何通知一个类(简单的是/否)? - C# How to notify a class (simple true/false) when a static method in another class has been invoked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM