“存根"是什么意思?在编程中是什么意思? [英] What does "to stub" mean in programming?

查看:55
本文介绍了“存根"是什么意思?在编程中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,这句话是什么意思?

For example, what does it mean in this quote?

与外部 API 集成几乎是任何现代网络应用程序的保证.为了有效地测试这种集成,您需要存根.一个好的存根应该易于创建并且始终与实际的、当前的 API 响应保持同步.在这篇博文中,我们将概述使用存根用于外部 API 的测试策略.

Integrating with an external API is almost a guarantee in any modern web app. To effectively test such integration, you need to stub it out. A good stub should be easy to create and consistently up-to-date with actual, current API responses. In this post, we’ll outline a testing strategy using stubs for an external API.

推荐答案

存根是现有依赖项(或合作者)的可控替代品在系统中.通过使用存根,您可以测试您的代码,而无需直接处理依赖.

A stub is a controllable replacement for an Existing Dependency (or collaborator) in the system. By using a stub, you can test your code without dealing with the dependency directly.

外部依赖 - 现有依赖:
它是您系统中的一个对象,您的代码under test 与您无法控制的交互.(常见的例如文件系统、线程、内存、时间等.)

例如在下面的代码中:

External Dependency - Existing Dependency:
It is an object in your system that your code under test interacts with and over which you have no control. (Common examples are filesystems, threads, memory, time, and so on.)

Forexample in below code:

public void Analyze(string filename)
    {
        if(filename.Length<8)
        {
            try
            {
                errorService.LogError("long file entered named:" + filename);
            }
            catch (Exception e)
            {
                mailService.SendEMail("admin@hotmail.com", "ErrorOnWebService", "someerror");
            }
        }
    }

您想测试 ma​​ilService.SendEMail() 方法,但为此您需要在测试方法中模拟 Exception,因此您只需要创建一个伪造 Stub errorService 对象来模拟您想要的结果,然后您的测试代码将能够测试 ma​​ilService.SendEMail() 方法.如您所见,您需要模拟来自另一个依赖项的结果,该依赖项是 ErrorService 类对象(现有依赖项对象).

You want to test mailService.SendEMail() method, but to do that you need to simulate an Exception in your test method, so you just need to create a Fake Stub errorService object to simulate the result you want, then your test code will be able to test mailService.SendEMail() method. As you see you need to simulate a result which is from an another Dependency which is ErrorService class object (Existing Dependency object).

这篇关于“存根"是什么意思?在编程中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆