使用 ref 参数模拟方法的语法

发布于 2024-11-04 21:04:33 字数 744 浏览 7 评论 0原文

我遇到的问题是无法模拟具有 ref 参数的方法。 我想要模拟的方法的签名如下:

class ContractRepository
...
public long GetValueAndIncrement(ref Counter counter)
{
 ...
}

我尝试像这样模拟它:

Random myRandomizer = new Random();
var contractRepo = new SIContractRepository();
contractRepo.GetValueAndIncrementCounterRef = ((internalCounter) => Int64.Parse(myRandomizer.Next().ToString()));

但是编译器告诉我我缺少“ref”关键字,但是当我这样尝试时,

Random myRandomizer = new Random();
var contractRepo = new SIContractRepository();
contractRepo.GetValueAndIncrementCounterRef = ((ref internalCounter) => Int64.Parse(myRandomizer.Next().ToString()));

我收到一个错误,即 ref是一个无效的表达式

不幸的是,谷歌在这里没有帮助。 :( 有什么想法吗?

I have the problem that I can't mock a method that has a ref argument.
The signature of the method I want to mock away is as follows:

class ContractRepository
...
public long GetValueAndIncrement(ref Counter counter)
{
 ...
}

I tried to mock it like this:

Random myRandomizer = new Random();
var contractRepo = new SIContractRepository();
contractRepo.GetValueAndIncrementCounterRef = ((internalCounter) => Int64.Parse(myRandomizer.Next().ToString()));

But the compiler tells me that I am missing the "ref" keyword, but when I try it like this

Random myRandomizer = new Random();
var contractRepo = new SIContractRepository();
contractRepo.GetValueAndIncrementCounterRef = ((ref internalCounter) => Int64.Parse(myRandomizer.Next().ToString()));

I get an error that ref is an invalid expression

Unfortunately, google doesn't help here. :(
Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一抹微笑 2024-11-11 21:04:33

在这种情况下,您根本无法使用匿名方法,因为它们既不支持 ref 也不支持 out 参数。您需要创建一个“真实”的方法。

public void SetupMock()
{
    Random myRandomizer = new Random();
    var contractRepo = new SIContractRepository();
    contractRepo.GetValueAndIncrementCounterRef = GetValueAndIncrementMock;
}

public long GetValueAndIncrementMock(ref Counter counter)
{
    return Int64.Parse(myRandomizer.Next().ToString())
}

You simply can't use anonymous methods in this case, because they support neither ref nor out parameters. You need to create a "real" method.

public void SetupMock()
{
    Random myRandomizer = new Random();
    var contractRepo = new SIContractRepository();
    contractRepo.GetValueAndIncrementCounterRef = GetValueAndIncrementMock;
}

public long GetValueAndIncrementMock(ref Counter counter)
{
    return Int64.Parse(myRandomizer.Next().ToString())
}
新人笑 2024-11-11 21:04:33

可以通过ref关键字使用匿名方法,只需在匿名方法中显式指定类型即可:

(ref Counter internalCounter) => Int64.Parse(myRandomizer.Next().ToString())

You can use anonymous methods with the ref keyword, just explicitly specify the type in the anonymous method:

(ref Counter internalCounter) => Int64.Parse(myRandomizer.Next().ToString())
若水微香 2024-11-11 21:04:33

请记住,当前版本的 Moles 仅支持 ref 和 out 参数作为方法的 LAST 参数。

http://research.microsoft.com/en-us/projects/pex /molesmanual.pdf

限制
目前 Moles 的实现有几个限制。这些限制并不
该方法固有的,可能会在 Moles 的未来版本中得到解决:
 Moles 框架仅支持有限数量的方法签名——最多
10 个参数,其中最后一个参数可以是 out 或 ref 参数。
不支持带有指针的方法签名。
 密封类或静态方法不能被存根,因为存根类型依赖于
虚拟方法调度。对于这种情况,请使用“摩尔”中描述的摩尔类型
类型”见本文档后面的内容

Remember that the current version of Moles only supports ref and out arguments as the LAST argument of a method.

http://research.microsoft.com/en-us/projects/pex/molesmanual.pdf

Limitations
The current implementation of Moles has several limitations. These limitations are not
inherent to the approach and might be resolved in future releases of Moles:
 The Moles framework supports only a limited number of method signature—up to
10 arguments, where the last argument can be an out or ref argument.
Method signatures with pointers are not supported.
 Sealed classes or static methods cannot be stubbed because stub types rely on
virtual method dispatch. For such cases, use mole types as described in “Mole
Types” later in this document

小耗子 2024-11-11 21:04:33

我不确定这是否是涂抹痣的正确方法,但我做到了。它有效。

///method get call in unit test
public static void DetermineSprintCorporateLiableCustomer()
{
  COptions p2 = new COptions();  
  MGetCOptions.CustomerInfoCallerOptionsRef = (ref COptions p1) =>
  {
    if (p1 != null && p1 != null && p1.Type.Equals(
      "Data", StringComparison.OrdinalIgnoreCase))
      {
        p1.Type = "P";
        p1.Indicator = true; 
      }
    p2 = p1;
  };
}

当这部分在测试运行期间执行时,新的p2可用。以下是我的场景。

// need to unit test Coptions.Type="Data"
public static MainMethod(Coptions)
{
  Mclass.Method(ref Coptions);
  If(COptions.Type="B")
    Do something();
} 

它具有新的价值,但可能有更好的方法。

I am not sure whether this is a correct way to apply moles but I did it. And it works.

///method get call in unit test
public static void DetermineSprintCorporateLiableCustomer()
{
  COptions p2 = new COptions();  
  MGetCOptions.CustomerInfoCallerOptionsRef = (ref COptions p1) =>
  {
    if (p1 != null && p1 != null && p1.Type.Equals(
      "Data", StringComparison.OrdinalIgnoreCase))
      {
        p1.Type = "P";
        p1.Indicator = true; 
      }
    p2 = p1;
  };
}

When this part executed during test run, new p2 is available. Below was my scenario.

// need to unit test Coptions.Type="Data"
public static MainMethod(Coptions)
{
  Mclass.Method(ref Coptions);
  If(COptions.Type="B")
    Do something();
} 

It works with new value but there could be a better way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文