Luis Aguilar
Luis Aguilar

Reputation: 4371

Mocking Out Parameter In Callback Method

So, I have a method with this signature:

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

and I wanna setup expectation so I can check that the filter parameter is null. The problem comes with the final out paremter. My current expectation setup is like this:

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;
   });

No luck, however. The setup crash with an exception saying Callback arguments didn't match the method arguments. Any suggestion on how to do this? Is there a way to just use the first argument and skip the rest?

Thanks.

Upvotes: 4

Views: 2250

Answers (2)

KornMuffin
KornMuffin

Reputation: 2957

You may be running into the following:

"A lambda expression cannot directly capture a ref or out parameter from an enclosing method."

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

Edit: You can create / use a custom delegate ... for example declare...

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

then in your test...

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 );
        }

Upvotes: 4

Zenwalker
Zenwalker

Reputation: 1919

Use the optional parameters avail in c#

Upvotes: 0

Related Questions