NSubstitute, out 参数和条件返回 [英] NSubstitute, out Parameters and conditional Returns

查看:67
本文介绍了NSubstitute, out 参数和条件返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与下面我想在 NSubstitute 中模拟的场景不同的场景......

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

...使用此接口,我希望它采用文件名并检索字节数据.使用 NSubstitute 我想向它传递特定的文件名并返回不同的或空的字节数组.通常,我会通过在我的脚手架中指定参数并根据需要使用Returns"元素来实现此用途.不幸的是,我不能使用 'Arg.Any()' 参数,因为它出现了......

...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...

ref 或 out 参数必须是可赋值的变量

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!

简而言之,我需要声明 'out' 参数,但我必须为我的模拟调用分配一个变量 - 这在特定的 'Returns' 语句中无法识别.是否有可能实现类似...

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.

提前致谢.

推荐答案

这未经过测试,但也许您可以使用Arg.Is<>",如下例所示:

This is not tested, but perhaps you can utilize "Arg.Is<>", as in this example:

IGrabSomeData grabSomeData_1 = Substitute.For<IGrabSomeData>();

byte[] empty = { };
grabSomeData_1.GrabThatData(Arg.Is<string>(x => string.Equals(x, "test1.xml")), out empty).Returns(x => { x[1] = new byte[] { 0, 1, 2 }; return true; });
grabSomeData_1.GrabThatData(Arg.Is<string>(x => string.Equals(x, "test2.xml")), out empty).Returns(x => { x[1] = new byte[] { 0, 1, 2, 3, 4 }; return true; });
grabSomeData_1.GrabThatData(Arg.Is<string>(x => string.Equals(x, "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);

这篇关于NSubstitute, out 参数和条件返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆