x[0]
refers to the first argument passed to NextBytes
, in your case the buffer
parameter. As the parameter is not ref
or out
, changing the array reference within your mocked member won´t have any effect to the calling code. So effectively you're doing this:
class TheMoc : IRandomNumberGenerator
{
public void NextBytes(byte[] bytes)
{
bytes = new byte[] {0x4d, 0x65, 0x64, 0x76};
}
}
which of course won't be reflected in the calling code. This is why NSubsitute gives you the exception.
Having said this it's not really clear why you want this, as you're calling code will never reflect whatever your actual implementation of the interface does with that array.
So when your calling code looks like this:
theGenerator.NextBytes(bytes);
that code will still reference the "old" array, not the "new" one (which you try to mock away).
So you will need to provide the param as ref
or out
to reflect that modification in your calling code.
If you know that your array will allways have four elements you could just modifiy the arrays content, not its reference:
randomImplementation.When(x => x.NextBytes(Arg.Any<byte[]>())).Do(x =>
{
x[0][0] = 0x4d;
x[0][1] = 0x65;
x[0][2] = 0x64;
x[0][3] = 0x76;
});
So you do not want to match any call to NextBytes
, but only those that provide an array with four bytes, making your Arg.Any<byte[]()>
effectivly a Arg.Is<byte[]>(x => x.Lengh == 4)
.