我使用CAKE 0.21.1.0。將CakeContext傳遞給另一個.cake文件
我的build.cake
腳本加載另一個.cake
腳本:tests.cake
。
在tests.cake
,我有一個類叫TestRunner
。 TestRunner
有一個稱爲RunUnitTests()
的方法,該方法使用VSTest
方法provided by CAKE執行單元測試。
在build.cake
中,我創建了幾個TestRunner
的實例。每當我調用的實例中的任何一個RunUnitTests()
方法,我看到了以下錯誤消息:
error CS0120: An object reference is required for the non-static field, method, or property 'VSTest(IEnumerable<FilePath>, VSTestSettings)'
我想這是因爲我需要在tests.cake
調用VSTest
上CakeContext
明確的實例。
我的問題是:如何確保我的tests.cake
腳本共享相同的CakeContext
實例作爲我的build.cake
腳本?我應該怎麼做才能編譯tests.cake
?
編輯:
針對devlead's reply,我決定添加更多的信息。
我跟着devlead的建議,改變了我的RunUnitTests()
方法簽名:
public void RunUnitTests(ICakeContext context)
在build.cake
,我的任務之一將執行以下操作:
TestRunner testRunner = TestRunnerAssemblies[testRunnerName];
testRunner.RunUnitTests(this);
其中TestRunnerAssemblies
是隻讀辭典tests.cake
和testRunnerName
是以前定義的變量。 (在build.cake
,我已插入#l "tests.cake"
。)
現在我看到此錯誤消息:
error CS0027: Keyword 'this' is not available in the current context
我在做什麼錯?
編輯:
沒關係,我需要學習如何更仔細地閱讀。而不是通過this
,而是通過Context
代替,正如devlead最初的建議。現在可以調用RunUnitTests
方法而沒有問題。
不要使用'this',而是使用'Context'如下所示:https://github.com/cake -contrib/Cake.Recipe/blob/develop/setup.cake#L13以及@devlead示例 –