首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NMock方法混合WithArguments和WithAnyArguments

NMock方法混合WithArguments和WithAnyArguments
EN

Stack Overflow用户
提问于 2014-08-07 08:53:51
回答 1查看 449关注 0票数 0

执行单元测试时,我在模拟一个方法时出错了。我试图根据要输入的参数返回不同的结果,否则返回一个默认的答案。问题是,我总是收到默认的答案。

代码语言:javascript
运行
复制
_commonFunctionsMock.Expects.AtLeastOne.Method(x => x.GetFeePrice("", false, null))
            .WithAnyArguments().WillReturn(-1);
        _commonFunctionsMock.Expects.AtLeastOne.Method(x => x.GetFeePrice("",false,null))
            .With("BAG1", true, FamilyFaresType.Optima).WillReturn(0);
        _commonFunctionsMock.Expects.AtLeastOne.Method(x => x.GetFeePrice("", false, null))
            .With("BAG2", true, FamilyFaresType.Optima).WillReturn(87);
        _commonFunctionsMock.Expects.AtLeastOne.Method(x => x.GetFeePrice("", false, null))
            .With("BAG3", true, FamilyFaresType.Optima).WillReturn(139);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-07 08:53:51

错误在于行的顺序。行返回默认的"WithAnyArguments“必须在带有参数" with”的定义之后。

代码语言:javascript
运行
复制
            _commonFunctionsMock.Expects.AtLeastOne.Method(x => x.GetFeePrice("",false,null))
            .With("BAG1", true, FamilyFaresType.Optima).WillReturn(0);
        _commonFunctionsMock.Expects.AtLeastOne.Method(x => x.GetFeePrice("", false, null))
            .With("BAG2", true, FamilyFaresType.Optima).WillReturn(87);
        _commonFunctionsMock.Expects.AtLeastOne.Method(x => x.GetFeePrice("", false, null))
            .With("BAG3", true, FamilyFaresType.Optima).WillReturn(139);
        _commonFunctionsMock.Expects.AtLeastOne.Method(x => x.GetFeePrice("", false, null))
            .WithAnyArguments().WillReturn(-1);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25178163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档