I mock a method from an interface which has optional parameters to return true
.
However, depending on whether I pass all the optional parameters to the call or only the required parameters I get a different result.
以下是该行为的完整工作示例:
using Moq;
using System.Threading;
using System.Threading.Tasks;
using Xunit.Abstractions;
namespace MoqTest
{
public interface IProcedureService
{
Task<bool> InterfaceMethod(string req1, int req2, int? opt1 = null, CancellationToken opt2 = default);
}
public class Tests
{
Mock<IProcedureService> _procedureService = new Mock<IProcedureService>();
ITestOutputHelper _output;
public Tests(ITestOutputHelper output) => _output = output;
[Xunit.Fact]
public async void DoTheTest()
{
_procedureService.Setup(x => x.InterfaceMethod(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(true);
_output.WriteLine($"result from IsUniqueProcedureNameInWorkspace (using optional defaults) " +
$"{(await _procedureService.Object.InterfaceMethod("", 0))}"); // returns false
_output.WriteLine($"result from IsUniqueProcedureNameInWorkspace (NOT using optional defaults) " +
$"{(await _procedureService.Object.InterfaceMethod("", 0, 0, default))}"); // returns true
}
}
}
这将输出以下内容:
Standard Output:
result from IsUniqueProcedureNameInWorkspace (using optional defaults) False
result from IsUniqueProcedureNameInWorkspace (NOT using optional defaults) True
I would expect them to both return True
as I set It.IsAny
for all the parameters in the Setup.
What am I missing here?
我的Moq版本是4.20.70