【问题标题】:Ballerina - error when running mock testsBallerina - 运行模拟测试时出错
【发布时间】:2018-10-02 09:58:37
【问题描述】:

更新

您好,我正在 Ballerina 中编写如下测试函数(在包通知中)。

import ballerina/test;
import ballerina/io;

@test:Mock {
    packageName: "notifications",
    functionName: "getMissingIds"
}
function mockGetMissingCount() returns (int) {
    int count = 3;

    return count;
}

@test:Config
function testAssertArrayEquals() {
    int answer = 0;
    string[] expectedResult = 3;
    string[] actualResult = getMissingCount();
    io:println("Function mocking test");
    test:assertEquals(actualResult, expectedResult, msg = "function mocking failed");
}   

我在与 notificationtest.bal 文件相同的包中的另一个文件中有实际的函数 getMissingCount。但是,当我通过执行

来运行测试时
ballerina test notifications

调用实际的 getMissingCount 函数而不是模拟函数。有谁知道为什么会发生这种情况?解决方法是什么。

【问题讨论】:

  • 你能检查getMissingCount函数是否公开吗?

标签: ballerina


【解决方案1】:

我认为这里的问题是当您运行依赖于一些封闭包级别功能的测试时,您必须在包级别运行测试。

例如在上面的例子中,如果你的包名是foo,并且在里面你有所有的ballerina源文件,也有上面例子中的测试用例,那么你必须以ballerina test foo运行测试将执行在包foo 中找到的测试。

使用 ballerina test <bal-file> 会将 bal 文件视为单个实体,它不会知道您的封闭包/项目。因此,在这种情况下,您可能必须将所有函数都放在同一个 bal 文件中才能使其工作。

【讨论】:

  • 是的,我必须将测试包含在一个“测试”文件夹中,该文件夹包含在包含我需要测试的功能的包中。然后它起作用了。
  • @Krishanthan :当我运行测试时,实际的 getMissingIds 函数被调用而不是模拟函数。你知道怎么调用mock函数吗?
【解决方案2】:

请参阅此 - https://ballerina.io/learn/by-example/testerina-function-mocks.html。您的模拟函数缺少注释@test:Mock。您还需要在 @test:Mock 注释中声明包名称。

// Mock 'getMissingIds' function of 'notifications' package.
@test:Mock {
    packageName: "notifications",
    functionName: "getMissingIds"
}
function mockGetMissingCount() returns (int) {
    int count = 3;
    return count;
}

@test:Config
function testAssertArrayEquals() {
    int answer = 0;
    string[] expectedResult = 3;
    string[] actualResult = getMissingCount();
    io:println("Function mocking test");
    test:assertEquals(actualResult, expectedResult, msg = "function mocking failed");
}

我没有运行上面的。但请尝试。

【讨论】:

猜你喜欢
  • 2014-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多