0

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

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

1 回答 1

1

错误在于行的顺序。该行返回默认的“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);
于 2014-08-07T08:53:51.887 回答