Search code examples
rhino-mocks

Rhino Mocks: Fill array argument in stub without "ref" keyword


I have a problem filling an array argument without the ref keyword.

For this signature:

int ReceiveData(ref byte[] data)

this works:

byte[] dataParameter = new byte[8];
byte[] testData = new byte[] { 1, 2, 3, 4 };
this.mockObject.Stub(t => t.ReceiveData(ref dataParameter)).OutRef(testData).Return(4);

After calling "ReceiveData" the "data" argument has the value { 1, 2, 3, 4 }. What should I do to fill the data argument for this signature:

int ReceiveData(byte[] data)

OutRef() doesn't work for this. Any idea? Thanks...


Solution

  • I was just having a similar problem and found my solution with the 'Callback' method. In my case, the input buffer argument is not declared 'ref', so might not be quite right for you, but I think it should work.

    For example;

    mockObject.Expect(x => x.ReceiveData(Arg<byte[]>.Is.NotNull)).Callback((byte[] buffer) =>
    {
       Array.Copy(outBytes, 0, buffer, 0, outBytes.Length);
       return true;
    }).Return(10); // return some number of bytes received, and set the bytes in the callback