繁体   English   中英

Jasmine JavaScript测试-等待功能测试完成

[英]Jasmine JavaScript test - wait for a function test to finish

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

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

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

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

根据成功/失败,我的规格(方案)失败或通过。

测试这三件事的问题是:每件事都要花时间,尤其是数字3。到目前为止,我已经尝试过茉莉花,并且无法弄清楚如何进行这样的顺序测试,因此每个步骤(测试)都必须完成下一个开始。 也许有一个更好的框架可以做到这一点? 我只测试JavasScript,不测试界面(按钮,文本字段等)。

这是我的基本情况:

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

该模板的结果

因此,基本上,我需要确保它们一个接一个运行,并且下一个在完成前一个运行之后运行。 假设,“测试电话”已完成,然后测试“到服务器的关闭连接”是否成功。

基本上,您在测试用例中获取了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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM