2013-08-30 8 views
2

FakeItEasy를 처음 사용하고 처음 시도했을 때 생소했습니다. 내가 가짜 원하는 인터페이스는이 같은 방법이있다 : I는 인수가 전달되는 방법을보고 싶습니다FakeItEasy Returns 매개 변수를 사용하여 짧게 반환

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 부분을 변경하는 경우 : 그렇지처럼이 읽기, 나 같은 초보자

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 

:

.ReturnsLazily((string path, int offset, int count, out long size) => 
    { 
     size = 0; 
     return Encoding.UTF8.GetBytes("You requested: " + path + "(" + offset + "," + count + ")"); 
    }) 

을가 컴파일되지 않습니다, 나는 오류를 이해하지 못하는 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의 용도 근처에?

답변

1

업데이트 : 아래에서 언급 한 문제는 FakeItEasy 릴리즈 1.15에서 수정되었으므로 최신 버전으로 업데이트하면 ReturnsLazily을 사용할 때 메소드의 out/ref 지정자에 대해 걱정할 필요가 없습니다.

죄송합니다. 적어도 당신에게 도움이되는 솔루션을 발견하게되어 기쁩니다.

내가 착륙 한 구문이 가장 읽기 쉽지 않지만 올바른 것으로 판단됩니다. ReturnsLazily의 "편의"버전은 람다가 out/ref 수정자를 취할 수 없기 때문에 out/ref 매개 변수와 함께 작동하지 않습니다.

오늘은 도움이되지 않지만 문제를 해결하기 위해 FakeItEasy issue 168을 만들었습니다. 추가 의견이 있으시면, 의견을 나누거나 의견을 제시하십시오.

3

귀하의 통제하에있는 인터페이스입니까?

byte[] ReadFileChunk(string path, int offset, int count, out long size); 

그렇다면 다음 byte[] 반환의 크기와 똑같이 out long size 아닌가? 그런 경우 인터페이스 메서드에서 size 매개 변수를 제거하고 "nice-to-read"ReturnsLazily 메서드를 먼저 사용하십시오.