2017-09-29 12 views
2

CAKE 0.21.1.0을 사용합니다.다른 .cake 파일에 CakeContext 전달

build.cake 스크립트에 다른 .cake 스크립트 : tests.cake이로드됩니다.

tests.cake에는 TestRunner이라는 클래스가 있습니다. TestRunner에는 RunUnitTests()이라는 메서드가 있으며 VSTest 메서드 provided by CAKE을 사용하여 단위 테스트를 실행합니다.

build.cake에서 나는 TestRunner의 몇 가지 인스턴스를 만듭니다.

error CS0120: An object reference is required for the non-static field, method, or property 'VSTest(IEnumerable<FilePath>, VSTestSettings)' 

내가 tests.cakeCakeContext의 명시 적 인스턴스 VSTest를 호출해야하기 때문에이 생각 : 나는 인스턴스 중 하나에 RunUnitTests() 메소드를 호출 할 때마다, 나는 다음과 같은 오류 메시지가 표시됩니다.

내 질문 : 내 tests.cake 스크립트가 CakeContext 인스턴스를 내 build.cake 스크립트와 공유하도록하려면 어떻게해야합니까? 컴파일하려면 tests.cake을 얻으려면 어떻게해야합니까?

편집 :

devlead's reply에 대응하여, 좀 더 정보를 추가하기로 결정했습니다.

내가 devlead의 제안을 따라 내 RunUnitTests() 방법 서명을 변경 : TestRunnerAssemblies 읽기 전용 사전이다

TestRunner testRunner = TestRunnerAssemblies[testRunnerName]; 
testRunner.RunUnitTests(this); 

을 :

public void RunUnitTests(ICakeContext context) 

build.cake에서, 내 작업 중 하나는 다음과 같은 작업을 수행합니다 tests.caketestRunnerName은 이전에 정의 된 변수입니다. (build.cake, 나는 #l "tests.cake"를 삽입 한.)

지금이 오류 메시지가 표시 :

error CS0027: Keyword 'this' is not available in the current context 

내가 잘못하고있는 중이 야 무엇을?

편집 :

는 좀 더주의 깊게 읽는 방법을 배울 필요, 신경 쓰지 마. this을 전달하는 대신, 원래 제안 된 devlead처럼 Context을 대신 전달했습니다. 이제 RunUnitTests 메소드를 문제없이 호출 할 수 있습니다.

+0

다음과 같이 this'는 대신,'Context'을 사용'사용하지 마십시오 https://github.com/cake -contrib/Cake.Recipe/blob/develop/setup.cake # L13 또한 @devlead 예제에 표시된대로 –

답변

4

RunUnitTests()이 정적 메서드이거나 클래스 인 경우 다른 범위이므로 RunUnitTests(ICakeContext context)과 같은 컨텍스트로 매개 변수로 컨텍스트를 전달해야합니다.

그런 다음 별칭을 해당 메서드의 확장으로 실행할 수 있습니다.

예 : 클래스

RunUnitTests(Context); 

public static void RunUnitTests(ICakeContext context) 
{ 
    context.VSTest(...) 
} 

예 :

Task("Run-Unit-Tests") 
    .Does(TestRunner.RunUnitTests); 

RunTarget("Run-Unit-Tests"); 


public static class TestRunner 
{ 
    public static void RunUnitTests(ICakeContext context) 
    { 
     context.VSTest("./Tests/*.UnitTests.dll"); 
    } 
} 
+0

devlead, 도와 줘서 고마워! 귀하의 회신에 대한 응답으로 내 게시물을 업데이트했습니다. –

+0

첫 번째 예제를 읽으면'this'는'Context'를 보내지 않습니다. – devlead

+1

답장을 보내 주셔서 감사합니다. 더 신중하게 읽는 법을 배워야합니다 :-) –