Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NSubstitute是否允许在同一方法上进行多种安排?_C#_Mocking_Nsubstitute - Fatal编程技术网

C# NSubstitute是否允许在同一方法上进行多种安排?

C# NSubstitute是否允许在同一方法上进行多种安排?,c#,mocking,nsubstitute,C#,Mocking,Nsubstitute,我是新人。测试失败是因为第二种安排导致调用第一种安排,甚至在“act”之前就失败了。我不确定我是否应该在同一种方法上做出多种安排。但是我觉得不管怎样,它都不应该调用第一个排列,因为参数不匹配 public interface IMediator { Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = defa

我是新人。测试失败是因为第二种安排导致调用第一种安排,甚至在“act”之前就失败了。我不确定我是否应该在同一种方法上做出多种安排。但是我觉得不管怎样,它都不应该调用第一个排列,因为参数不匹配

public interface IMediator
{
    Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default(CancellationToken));

    Task Send(IRequest request, CancellationToken cancellationToken = default(CancellationToken));

    Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default(CancellationToken))
        where TNotification : INotification;
}

public class MyMessage : IRequest<MyResponse> {}

public class MyResponse {}

public class MyMessage2 : IRequest<MyResponse> {}

    [Fact]
    public async Task Mock_Fail() {
        var mediatr = Substitute.For<IMediator>();
        var myMessage = new MyMessage();
        var myMessage2 = new MyMessage();
        var myResponse = new MyResponse();
        var myResponse2 = new MyResponse();

        mediatr.Send(Arg.Any<MyMessage>())
            .Returns((ci) => {
                Assert.Same(myMessage, ci[0]); //That fails
                return myResponse;
            });

        mediatr.Send(Arg.Any<MyMessage2>())
            .Returns((ci) => {
                return myResponse2;
            });

       //Execution never reaches here
        var response = await mediatr.Send(myMessage);
        var response2 = await mediatr.Send(myMessage2);

    }
公共接口IMediator
{
任务发送(IRequest请求,CancellationToken CancellationToken=default(CancellationToken));
任务发送(IRequest请求,CancellationToken CancellationToken=default(CancellationToken));
任务发布(t通知通知,CancellationToken CancellationToken=默认值(CancellationToken))
其中t通知:INotification;
}
公共类MyMessage:IRequest{}
公共类MyResponse{}
公共类MyMessage2:IRequest{}
[事实]
公共异步任务模拟失败(){
var mediatr=替换为();
var myMessage=new myMessage();
var myMessage2=新的MyMessage();
var myResponse=新的myResponse();
var myResponse2=新的MyResponse();
mediatr.Send(Arg.Any())
.返回((ci)=>{
Assert.Same(myMessage,ci[0]);//失败
返回myResponse;
});
mediatr.Send(Arg.Any())
.返回((ci)=>{
返回myResponse2;
});
//这里永远不会有死刑
var response=wait mediatr.Send(myMessage);
var response2=等待mediatr.Send(myMessage2);
}

通常我会像这样测试:

 mediatr.Send(myMessage)
        .Returns(ci => myResponse);

 mediatr.Send(myMessage2)
        .Returns(ci => myResponse2);
有几种方法可以覆盖以前抛出的存根,但我认为最好的方法是尽可能避免问题。:)

在提供更多信息后编辑
这看起来像。解决方法是使用
Arg.is(x=>x!=null)
,如该问题描述所示。总的来说,根据我最初的回答,我会更具体地关注存根,以避免电话重叠。

我也会这样做,但测试是为了证明NSubstitute的行为。重构它来解决问题没有意义:)你有更现实的问题例子吗?没有更多的背景,很难提出建议。通常,通过更改顺序来避免问题,最坏的情况是使用闭包/Func,您可以稍后更改或在回调中添加更多逻辑。或者看看When-Do语法是否有帮助。@Serguzest谢谢,看起来你发现了一个bug!用错误报告的链接更新了我的答案。我一定很幸运,这是我在Nsubstitute的第一天:)很抱歉你做了这么不吉利的介绍。:-\由于
null
是每个引用类型的成员,因此NSubstitute无法区分此处的调用,因此我不确定该错误的修复程度。很高兴能帮助您解决这个问题(根据我最初的回答,或通过更改安排顺序,使掷骰排在最后等),如果您需要更多信息,请向我/NSub小组发送电子邮件。