【问题标题】:Jasmine JavaScript test - wait for a function test to finishJasmine JavaScript 测试 - 等待函数测试完成
【发布时间】:2016-12-20 08:49:17
【问题描述】:

我正在尝试使用 Jasmine 来自动化我的 JavaScript 测试。我只是找不到关于一件事的信息(这是我想做的步骤):

  1. 登录到服务。 (返回成功或失败)

  2. 建立与服务器的连接。 (返回成功或失败)

  3. 进行测试 SIP 呼叫。 (返回成功或失败)

根据成功/失败,我的规范(场景)失败或通过。

测试这三件事的问题:每件事都需要时间,尤其是第三件事。到目前为止,我已经尝试过 Jasmine,并且可以弄清楚如何进行这样的顺序测试,所以每一步(测试)都有完成下一个开始。也许有更好的框架来做到这一点?我只是在测试 JavaScript,而不是界面(按钮、文本字段等)。

这是我的一个基本场景:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Jasmine Spec Runner v2.5.2</title>

    <!-- voxImplant stuff-->
    <script type="text/javascript" src="http://cdn.voximplant.com/voximplant.min.js"></script>

    <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.5.2/jasmine_favicon.png">
    <link rel="stylesheet" href="lib/jasmine-2.5.2/jasmine.css">

    <script src="lib/jasmine-2.5.2/jasmine.js"></script>
    <script src="lib/jasmine-2.5.2/jasmine-html.js"></script>
    <script src="lib/jasmine-2.5.2/boot.js"></script>

    <!-- include source files here... -->
    <script src="src/voximp_src.js"></script>    


    <!-- include spec files here... -->
    <script src="spec/voximp_spec.js"></script>

</head>

<body>     
</body>

</html>

// Make a test call to play MP3
describe("[VoxEngine Basic Call Test]", function () {
    it('does something', function (done) {
        VoxEngine.Login_to_a_service()
            .then(VoxEngine.Establish_a_connection)
            .then(VoxEngine.Make_a_test_call)
            .then(function () {
                expect(1).toEqual("SUCCESS");
                done();
            })
            .catch(fail)
    });    
});

window.VoxEngine = {
    Login_to_a_service: function () {        
        // Sleep
        var now = new Date().getTime();
        while (new Date().getTime() < now + 2000) { console.log("Login processing"); }

        console.log("Login done");
        return "SUCCESS";
    },

    Establish_a_connection: function () {
        // Sleep
        var now = new Date().getTime();
        while (new Date().getTime() < now + 2000) { console.log("Connection processing"); }

        console.log("Connection done");
        return "SUCCESS";
    },

    Make_a_test_call: function () {
        // Sleep
        var now = new Date().getTime();
        while (new Date().getTime() < now + 2000) { console.log("Call processing"); }

        console.log("call failed");
        return "FAIL";
    }
}

Result for this template

所以基本上,我需要确保它们一个接一个地运行,并且下一个在前一个完成之后运行。假设 Make-A-Test-Call 已完成,然后它测试 Connection-To-The-Server-Closed 是否成功。

【问题讨论】:

    标签: javascript testing automation jasmine automated-tests


    【解决方案1】:

    基本上,您在测试用例中获取 done 参数并在异步任务完成时调用它:

    it('does something', function(done) {
        Login_to_a_service()
            .then(Establish_a_connection)
            .then(Make_a_test_call)
            .then(function() {
               expect(1).toBe(1); // or something that makes more sense...
               done();
            })
            .catch(fail)
    });
    

    文档:https://jasmine.github.io/2.0/introduction.html#section-Asynchronous_Support

    【讨论】:

    • 谢谢。抱歉,我对任何 WebDev 的东西都是 100% 的新手。能否请您详细说明一下?
    • @YuriyL:当然,但我必须知道你不明白哪一部分。
    • 对不起!我已经编辑了我的帖子。请看看我那里有什么。
    • 所以基本上,我需要确保他们一个接一个地运行,下一个在前一个完成后运行。完成测试调用,然后测试与服务器的连接是否已正确终止。
    • @YuriyL:我猜voxInitvoxConnect 等不只是简单地返回“成功”,他们做了一些实际的工作。你能分享一下这些函数的实际调用方式吗?
    猜你喜欢
    • 2021-12-08
    • 2017-06-15
    • 2023-01-28
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 2017-06-01
    • 2013-06-27
    • 2018-01-16
    相关资源
    最近更新 更多