【发布时间】:2017-09-29 17:34:33
【问题描述】:
我使用 CAKE 0.21.1.0。
我的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 中的 CakeContext 的显式实例上调用 VSTest。
我的问题是:如何确保我的tests.cake 脚本与我的build.cake 脚本共享相同的CakeContext 实例?我应该怎么做才能让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
我做错了什么?
编辑:
没关系,我需要学习如何更仔细地阅读。正如devlead最初建议的那样,我没有传入this,而是传入Context。现在可以毫无问题地调用RunUnitTests 方法。
【问题讨论】:
-
不要使用
this,而是使用Context,如下所示:github.com/cake-contrib/Cake.Recipe/blob/develop/setup.cake#L13 以及@devlead 示例中所示