我是使用 FakeItEasy 的新手,我第一次尝试时就卡住了。我想伪造的界面有这样的方法:
byte[] ReadFileChunk(string path,int offset,int count,out long size);
我想看看参数是如何传递的,所以我使用 ReturnsLazily。这是我的尝试:
long resSize;
A.CallTo(() => dataAttributesController
.ReadFileChunk(A<string>.Ignored, A<int>.Ignored, A<int>.Ignored, out resSize))
.ReturnsLazily((string path, int offset, int count) =>
{
return Encoding.UTF8.GetBytes("You requested: " + path + "(" + offset + "," + count + ")");
})
.AssignsOutAndRefParameters(123);
这会编译,但运行时会生成此异常:
The faked method has the signature (System.String, System.Int32, System.Int32, System.Int64&), but returns lazily was used with (System.String, System.Int32, System.Int32).
这是正确的,但我不知道如何添加 out 参数。如果我将 ReturnLazily 部分更改为:
.ReturnsLazily((string path, int offset, int count, out long size) =>
{
size = 0;
return Encoding.UTF8.GetBytes("You requested: " + path + "(" + offset + "," + count + ")");
})
它不会编译,我不明白错误:
error CS1593: Delegate 'System.Func<FakeItEasy.Core.IFakeObjectCall,byte[]>' does not take 4 arguments
error CS1661: Cannot convert lambda expression to delegate type 'System.Func<string,int,int,long,byte[]>' because the parameter types do not match the delegate parameter types
error CS1677: Parameter 4 should not be declared with the 'out' keyword
对于像我这样的新手来说,这看起来不喜欢 4 个参数,也不明白如何处理“out”。有人可以解释一下我应该如何阅读这些错误吗?一个工作示例也将非常受欢迎:-)
非常感谢!
- - 编辑 - -
这似乎有效:
A.CallTo(() => dataAttributesController
.ReadFileChunk(A<string>.Ignored, A<int>.Ignored, A<int>.Ignored, out resSize))
.ReturnsLazily(x =>
Encoding.UTF8.GetBytes("You requested: " + x.Arguments.Get<string>(0) + "(" + x.Arguments.Get<int>(1) + "," + x.Arguments.Get<int>(2) + ")"))
.AssignsOutAndRefParameters((long)123);
比我希望的可读性差一点,这是否接近 ReturnsLazily 的预期用途?