我正在尝试为一段使用拼写校正器的代码设置单元测试。我已经正确注入(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/