我使用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示例 –