【问题标题】:Passing CakeContext to another .cake file将 CakeContext 传递给另一个 .cake 文件
【发布时间】: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);

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

现在我看到了这个错误信息:

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

我做错了什么?

编辑:

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

【问题讨论】:

标签: c# cakebuild


【解决方案1】:

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

【讨论】:

  • 感谢您的帮助,开发领导!为了回应您的回复,我对我的帖子进行了一些更新。
  • 如果您阅读我的第一个示例,请不要发送this 发送Context
  • 感谢您的回复。我需要学习如何更仔细地阅读:-)
  • 好极了,它被排序了;)
猜你喜欢
  • 2015-05-22
  • 2020-12-25
  • 1970-01-01
  • 2021-12-31
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
  • 2019-01-31
相关资源
最近更新 更多