例如,这句话是什么意思?
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.
最佳答案
stub 是现有依赖项(或协作者)的可控替代品 在系统中。通过使用 stub ,您可以测试代码而无需 直接处理依赖关系。
外部依赖项 - 现有依赖项:
它是您系统中的一个对象,您的代码
被测试与您无法控制的交互。 (常见的
例如文件系统、线程、内存、时间等等。)
例如下面的代码:
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");
}
}
}
您想要测试 mailService.SendEMail() 方法,但要做到这一点,您需要在测试方法中模拟异常,因此您只需创建一个伪造Stub errorService 对象来模拟您想要的结果,然后您的测试代码将能够测试mailService.SendEMail() 方法。正如您所看到的,您需要模拟来自另一个依赖项的结果,该依赖项是 ErrorService 类对象(现有依赖项对象)。
关于terminology - "to stub"在编程中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9777822/