简体   繁体   中英

Jasmine JavaScript test - wait for a function test to finish

I am trying out Jasmine to automate my JavaScript testing. I just can't find info about one thing (here are steps I want to do):

  1. Login to a service. (returns SUCCESS or FAILED)

  2. Establish a connection to a server. (returns SUCCESS or FAILED)

  3. Make a test SIP call. (returns SUCCESS or FAILED)

Based on SUCCESS/FAILED, my Spec (scenario) fails or passes.

The problem in testing these 3 things: it takes time to do each, especially number 3. So far I have tried Jasmine and can'd figure out how to do a sequential tests like that, so each step (test) has to finish for next one to start. Maybe there is a better framework to do that? I am testing just JavasScript, not interface (buttons, text fields, etc.).

Here is a basic scenario I have:

 <!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

So basically, I need to make sure they run one after another and the next one runs after previous one has finished. Let's say, Make-A-Test-Call has finished and then it tests if Connection-To-The-Server-Closed succeeded.

Basically, you acquire the done argument in your test case and call it when an async task is complete:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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