混合了参数和任意参数的模拟方法

本文关键字:参数 模拟 方法 混合 任意 | 更新日期: 2023-09-27 18:17:06

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

_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);

混合了参数和任意参数的模拟方法

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

            _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);