I have a scenario not dissimilar to the one below that I would like to mock up in NSubstitute...
public interface IGrabSomeData
{
bool GrabThatData(string filename, out byte[] data);
}
...with this interface I would like it to take a filename and retreive the byte data. Using NSubstitute I would like to pass it specific filenames and return different or empty byte arrays. Typically I would do this use by specifying the parameters in my scaffolding and use the 'Returns' element as appropriate. Unfortunately I cannot use the 'Arg.Any()' parameter as it comes up with...
A ref or out argument must be an assignable variable
...but if I do that it consistently an empty byte array. Here's a quick demo using the Interface above...
IGrabSomeData grabSomeData_1 = Substitute.For<IGrabSomeData>();
IGrabSomeData grabSomeData_2 = Substitute.For<IGrabSomeData>();
// Doesn't work
byte[] empty = { };
grabSomeData_1.GrabThatData(Arg.Any<string>(), out empty).Returns(x => { x[1] = new byte[] { 0, 1, 2 }; return true; });
byte[] test1 = {};
var result1 = grabSomeData_1.GrabThatData("" , out test1);
Assert.IsTrue( test1.length > 0); // FAILS
// Does work
grabSomeData_2.GrabThatData(Arg.Any<string>(), out empty).ReturnsForAnyArgs(x => { x[1] = new byte[] { 0, 1, 2 }; return true; });
byte[] test2 = { };
var result2 = grabSomeData_2.GrabThatData("", out test2);
Assert.IsTrue(empty.Length > 0); // Success!
In short, I need to declare the 'out' parameter but I must assign a variable to my mocked call - and that doesn't get recognised on the specific 'Returns' statements. Is it possible to achieve something like...
IGrabSomeData grabSomeData_1 = Substitute.For<IGrabSomeData>();
byte[] empty = { };
grabSomeData_1.GrabThatData("test1.xml", out empty).Returns(x => { x[1] = new byte[] { 0, 1, 2 }; return true; });
grabSomeData_1.GrabThatData("test2.xml", out empty).Returns(x => { x[1] = new byte[] { 0, 1, 2, 3, 4 }; return true; });
grabSomeData_1.GrabThatData("test3.xml", out empty).Returns(x => { x[1] = new byte[] { 0, 1 }; return true; });
byte[] test1 = {};
var result1 = grabSomeData_1.GrabThatData("test1.xml", out test1);
Assert.IsTrue(test1.Length == 2);
// etc.
Thanks in advance.