如何存根静态方法?

发布于 2024-12-05 08:03:17 字数 478 浏览 2 评论 0 原文

我正在开发一个棕地应用程序,目前正在重构其中的一部分。我正在尝试以 TDD 方式执行此操作,但遇到了问题。我正在测试的部分代码

var siteLanguages = from sl in SiteSettings.GetEnabledSiteLanguages() select sl.LanguageID;

GetEnabledLanguages 具有以下签名的

public static List<LanguageBranch> GetEnabledSiteLanguages();

情况下依次调用数据访问代码来检索相关信息。到目前为止,我已经使用接口和 DI 在单元测试期间对此类依赖项使用不同的存根实现。但由于 GetEnabledSiteLanguages 方法是静态的,因此这不起作用。在这种情况下,“正确”的做法是什么?

I am working on a brownfield application and am currently refactoring part of it. I am trying to do this in a TDD fashion but am running into a problem. Part of the code I am testing does

var siteLanguages = from sl in SiteSettings.GetEnabledSiteLanguages() select sl.LanguageID;

where GetEnabledLanguages has the following signature

public static List<LanguageBranch> GetEnabledSiteLanguages();

it in turns calls data access code to retrieve the relevant information. Up untill now I have used a interface and DI to use a different stub implementation for these kind of dependencies during unit testing. But since the GetEnabledSiteLanguages method is static this will not work. What is the "correct" way to do it in this case?

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

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

发布评论

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

评论(4

稀香 2024-12-12 08:03:17

您可以创建一个实现接口的对象,并将其实现注入到使用 SiteSettings 类的类中。该接口声明的方法与您需要拦截的静态方法具有相同的签名。然后,您可以模拟测试接口并创建一个委托给实际代码的静态方法的实现:

public interface ISiteSettings
{
    public List<LanguageBranch> GetEnabledSiteLanguages()
}

public class ActualSiteSettings : ISiteSettings
{
    public List<LanguageBranch> GetEnabledSiteLanguages()
    {
         return SiteSettings.GetEnabledSiteLanguages();
    }
}

...在依赖类中:

public class DependentClass
{
    private ISiteSettings m_siteSettings;

    public DependentClass(ISiteSettings siteSettings)
    {
    m_siteSettings=siteSettings;
    }

    public void SomeMethod
    {
         var siteLanguages = from sl in m_siteSettings.GetEnabledSiteLanguages() select sl.LanguageID;
    }
}

you could create a object which implements an interface and inject an implementation of this into the class which uses the SiteSettings class. The interface declare the method with the same signature as the static method(s) you need to intercept. Then you could mock out the interface for tests and create a single implementation which delegates to the static method for the actual code:

public interface ISiteSettings
{
    public List<LanguageBranch> GetEnabledSiteLanguages()
}

public class ActualSiteSettings : ISiteSettings
{
    public List<LanguageBranch> GetEnabledSiteLanguages()
    {
         return SiteSettings.GetEnabledSiteLanguages();
    }
}

... in the dependent class:

public class DependentClass
{
    private ISiteSettings m_siteSettings;

    public DependentClass(ISiteSettings siteSettings)
    {
    m_siteSettings=siteSettings;
    }

    public void SomeMethod
    {
         var siteLanguages = from sl in m_siteSettings.GetEnabledSiteLanguages() select sl.LanguageID;
    }
}
橪书 2024-12-12 08:03:17

怎么样让你的方法像这样:

public static Func<List<LanguageBranch>> GetEnabledSiteLanguages = () => {
   //your code here
};

现在它成为第一类对象(作为 Func 委托)并且存根可以替换它

What about making your method such as:

public static Func<List<LanguageBranch>> GetEnabledSiteLanguages = () => {
   //your code here
};

Now it becomes first class object (as Func delegate) and a stub can replace it

小嗲 2024-12-12 08:03:17

查看 Moles 框架。

Look at Moles framework.

旧伤还要旧人安 2024-12-12 08:03:17

您可以使用 JustMockTypeMockmoles。这些工具允许您模拟一切,例如静态方法。

You can use tools like JustMock, TypeMock or moles. These tools allow you to mock everythings like static methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文