【问题标题】:Mocking Out Parameter In Callback Method在回调方法中模拟出参数
【发布时间】:2011-06-15 04:23:52
【问题描述】:

所以,我有一个带有这个签名的方法:

IList<Mail> FindFilteredPaged(
   QueryFilter filter, 
   int pageIndex, 
   int pageSize, 
   out int totalRecords);

我想设置期望值,以便检查filter 参数是否为空。问题来自最终的out 参数。我目前的期望设置是这样的:

Expect
   .Call(registryMailService.FindFilteredPaged(
      Arg<QueryFilter<IncomingMail>>.Is.Anything,
      Arg<Int32>.Is.Anything,
      Arg<Int32>.Is.Anything,
      out Arg<Int32>.Out(20).Dummy))                         
   .Callback<QueryFilter<IncomingMail>, Int32, Int32>((p1, p2, p3) => 
   {
       filterWasNotSpecified = p1 == null;
   });

不过,运气不好。安装程序崩溃,异常为Callback arguments didn't match the method arguments。关于如何做到这一点的任何建议?有没有办法只使用第一个参数并跳过其余的?

谢谢。

【问题讨论】:

    标签: c# asp.net rhino-mocks


    【解决方案1】:

    您可能会遇到以下情况:

    “lambda 表达式不能直接从封闭方法中捕获 ref 或 out 参数。”

    http://msdn.microsoft.com/en-us/library/bb397687.aspx

    编辑:您可以创建/使用自定义委托...例如声明...

    public delegate void SomeAction<T1, T2, T3>( out T1 a, ref T2 b, T3 c );
    

    那么在你的测试中...

    SomeAction<int, string, string> fakeDoSomething  = ( out int outParam, ref string refParam, string param ) =>
                {
                    outParam = 123;
                    refParam = "123";
                };
    
            using ( m_Mocks.Record() )
            {
                Expect.Call( () => m_MockService.DoSomething( out outInt, ref refString, someString ) ).Do( fakeDoSomething );
            }
    

    【讨论】:

    【解决方案2】:

    在c#中使用可选参数avail

    【讨论】:

      猜你喜欢
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多