NSubstitute - 检查传递给方法的参数 [英] NSubstitute - Check arguments passed to method

查看:57
本文介绍了NSubstitute - 检查传递给方法的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前正在从 RhinoMocks 迁移到 NSubstitute.

We are currently in the process of moving from RhinoMocks to NSubstitute.

我有一个方法接受 DatabaseParams 类型的对象.此类具有以下结构(简化):

I have a method that takes an object of type DatabaseParams. This class has the following structure (simplified):

public class DatabaseParams
  {
    public string StoredProcName { get; private set; }
    public SqlParameter[] Parameters { get; private set; }

    public DatabaseParams(string storeProcName, SqlParameter[] spParams)
    {
      StoredProcName = storeProcName;
      Parameters = spParams;
    }
  }

我有以下方法,我想检查传递给它的参数是否正确:

I have the following method I want to check the arguments being passed to it are correct:

public interface IHelper
{
Task<object> ExecuteScalarProcedureAsync(DatabaseParams data);
}

我如何测试 DatabaseParams 的实例是否以正确的值传递到该方法中?

How do I test that an instance of DatabaseParams was passed into that method with the correct values?

我可以在 RhinoMocks 中用这样的方法做到这一点:

I could do this in RhinoMocks with something like this:

helperMock.Expect(m => m.ExecuteScalarProcedureAsync(Arg<DatabaseHelperParameters>.Matches(
        p =>   p.StoredProcName == "up_Do_Something"
            && p.Parameters[0].ParameterName == "Param1"
            && p.Parameters[0].Value.ToString() == "Param1Value"
            && p.Parameters[1].ParameterName == "Param2"
            && p.Parameters[1].Value.ToString() == "Param2Value"
        ))).Return(Task.FromResult<DataSet>(null));

helperMock 正在模拟包含 ExecuteScalarProcedureAsync 方法的接口 IHelper.

The helperMock is mocking the interface IHelper that contains the ExecuteScalarProcedureAsync method.

推荐答案

我自己已经找到了答案.

I've figured out the answer myself.

NSubstitute 只需要使用 .Received() 调用,然后当您为方法指定参数时.您可以将匹配的参数指定为谓词.

NSubstitute just needs to use the .Received() call and then when you specify your argument to the method. You can specify the argument matching as a predicate.

例如:

  helperMock.Received().ExecuteScalarProcedureAsync(Arg.Is<DatabaseParams>(
   p =>   p.StoredProcName == "up_Do_Something"
        && p.Parameters[0].ParameterName == "Param1"
        && p.Parameters[0].Value.ToString() == "Param1Value"
        && p.Parameters[1].ParameterName == "Param2"
        && p.Parameters[1].Value.ToString() == "Param2Value"));

这篇关于NSubstitute - 检查传递给方法的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆