gpt4 book ai didi

c# - 是否可以针对任意输入训练 Rhinomocks?

转载 作者:行者123 更新时间:2023-11-30 22:35:04 24 4
gpt4 key购买 nike

我正在尝试为一段使用拼写校正器的代码设置单元测试。我已经正确地注入(inject)了代码依赖项,所以在 Rhinomocks 中设置 stub 不是问题,但是我为测试创建的文本 block 有 50 个字长,我真的宁愿没有 50 行代码看起来像这样:

spellingCorrector.Stub(x => x.CorrectWord("the")).Return("the");
spellingCorrector.Stub(x => x.CorrectWord("boy")).Return("boy");
spellingCorrector.Stub(x => x.CorrectWord("ran")).Return("ran");

出于单元测试的目的,我认为假设单词拼写正确是可以的。有没有办法让 Rhinomocks 简单地遵循关于返回的规则,效果是:

spellingCorrector.Stub(x => x.CorrectWord(y)).Return(y);

最佳答案

您可以使用 IgnoreArguments() 方法:

spellingCorrector
.Stub(x => x.CorrectWord(null))
.IgnoreArguments()
.Return(y);

这样无论传递给 CorrectWord 方法的值是什么,它都会返回 y


更新:

在你的评论之后它更清楚:

Func<string, string> captureArg = arg => arg;
spellingCorrector.Stub(x => x.CorrectWord(null)).IgnoreArguments().Do(captureArg);

这将使用作为参数传递的任何值作为返回值。如果您需要对此返回值执行一些转换,请调整 captureArg 委托(delegate)。

关于c# - 是否可以针对任意输入训练 Rhinomocks?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7573162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com