将 CakeContext 传递给另一个 .cake 文件

Passing CakeContext to another .cake file

我使用 CAKE 0.21.1.0.

我的 build.cake 脚本加载另一个 .cake 脚本:tests.cake.

tests.cake中,我有一个叫做TestRunner的class。 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 的建议并将我的 RunUnitTests() 方法签名更改为:

public void RunUnitTests(ICakeContext context)

build.cake 中,我的一项任务执行以下操作:

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

其中 TestRunnerAssembliestests.cake 中的只读字典,而 testRunnerName 是先前定义的变量。 (在build.cake中,我插入了#l "tests.cake"。)

现在我看到这条错误消息:

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

我做错了什么?

编辑:

没关系,我需要学习如何更仔细地阅读。正如 devlead 最初建议的那样,我没有传入 this,而是传入了 Context。现在可以毫无问题地调用 RunUnitTests 方法。

如果 RunUnitTests() 是静态方法或在 class 中,您需要像 RunUnitTests(ICakeContext context) 一样将上下文作为参数传递给它,因为它是不同的范围。

然后您可以执行别名作为该方法的扩展。

示例:

RunUnitTests(Context);

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

示例class:

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");
    }
}