使用 restler 在 jasmine E2E 测试中进行回调

callbacks in jasmine E2E testing using restler

我正在测试 restful api 使用 restler 和 jasmine nodejs 模块。我创建了测试文件来测试单个 api 调用和测试需要彼此输入的 api 调用组。我如何使用正确的 jasmine describe-it-expect 块格式将来自一个这样的 api 调用的反馈/输出提供给另一个 api 调用。如果我尝试将 verifyOtp 函数放在 getPhoneOtp 之后的另一个 it-block 中,那么它不会在成功获取 OTP 后执行,因此失败。现在我只能在控制台中读取输出,无法使用 jasmine expect 函数,因为我无法在 it-block 中包含 verifyOtp 函数。任何建议,将不胜感激 。这是代码。

var restler = require('restler');
var fs = require('fs');
var colors = require('colors');
var util = require('util');
var config = require('./config.js') ;
var baseUrl = config.baseUrl ;

describe("LEC phone api's tests", function () {

    var _token;
    var phone = Math.round(Math.random() * 1000000000);
    var otp;

    console.log("Test started on "+baseUrl) ;



    function verifyOtp(otpPassed) {

        var success = 0 ;
            restler.post(baseUrl + "phone/verifyotp", {
                data: {
                    _token: _token,
                    phoneNumber: phone,
                    otp: otpPassed

                },
                headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                }
            }).on("success", function (data) {
                success = 1 ;
                console.log(colors.blue("Success of verify otp")) ;

                console.log(data);

                var userInfo = {
                    firstName: "John",
                    lastName: "Doe",
                    description: "I'm a getting used for testing .",
                    zipCode: "71151",
                    street: "Testing avenue",
                    city: "San Fransisco",
                    state: "California",
                    email: "john@doe.com",
                    image: ""

                };


            }).on("complete", function (data) {
                if(!success)
                console.log(colors.red(util.inspect(data))) ;
            });
    };


    it("should get the token", function () {
        var success = 0 ;
        restler.get(baseUrl + "/basic/token")
            .on("success", function (data) {
                success = 1;
                console.log(colors.blue("Success of get the Token")) ;
                console.log(data);
                _token = data.token;


            }).on("complete", function (data) {
                if(!success)
                    console.log(colors.red(util.inspect(data))) ;
            });

    });

    it("should get the OTP", function () {
        var success = 0 ;
        restler.post(baseUrl + "phone/getphoneotp", {
            data: {
                _token: _token,
                phoneNumber: phone
            },
            headers: {
                'X-Requested-With': 'XMLHttpRequest'
            }
        }).on("success", function (data) {
            success = 1;
            console.log(colors.blue("Success of get the OTP")) ;
            console.log(data);
            otp = data.otp;

            verifyOtp(otp);
        }).on("complete", function (data) {
            if(!success)
                console.log(colors.red(util.inspect(data))) ;        });
    });

});

我阅读了一些关于 jasmine 中异步测试的堆栈溢出问题的答案,并且能够在这些答案的帮助下编写代码。这是代码。

var restler = require('restler');
var fs = require('fs');
var colors = require('colors');
var util = require('util');
var config = require('./config.js');
var baseUrl = config.baseUrl;

describe("LEC phone api's tests", function () {

    var apiCall;
    var _token;
    var phone = Math.round(Math.random() * 1000000000);
    var otp, otpPassed;

    console.log("Test started on " + baseUrl);
    it("should get the token", function () {


            restler.get(baseUrl + "/basic/token")
                .on("success", function (data) {
                    success = 1;
                    console.log(colors.blue("Success of get the Token"));
                    console.log(data);
                    _token = data.token;

                    apiCall = 1;

                }).on("complete", function (data) {
                    if (!success)
                        console.log(colors.red(util.inspect(data)));
                });

    });

    it("should get the otp", function () {

        waitsFor(function () {
            return apiCall == 1;
        });
        runs(function () {
            var success = 0;
            restler.post(baseUrl + "phone/getphoneotp", {
                data: {
                    _token: _token,
                    phoneNumber: phone
                },
                headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                }
            }).on("success", function (data) {
                success = 1;
                console.log(colors.blue("Success of get the OTP"));
                console.log(data);
                otp = data.otp;


                otpPassed = otp ;

                apiCall = 2 ;
            }).on("complete", function (data) {
                if (!success)
                    console.log(colors.red(util.inspect(data)));
            });
        });

    });

    it("should verify the otp", function () {


        var success = 0;
        waitsFor(function() {
           return apiCall == 2 ;
        });
        runs(function() {
            restler.post(baseUrl + "phone/verifyotp", {
                data: {
                    _token: _token,
                    phoneNumber: phone,
                    otp: otpPassed

                },
                headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                }
            }).on("success", function (data) {
                success = 1;
                console.log(colors.blue("Success of verify otp"));

                console.log(data);

                apiCall = 3 ;

            }).on("complete", function (data) {
                if (!success)
                    console.log(colors.red(util.inspect(data)));
            });

        });
    });



    it("should register the user", function () {

        var success = 0;
        var userInfo = {
            firstName: "John",
            lastName: "Doe",
            description: "I'm a getting used for testing .",
            zipCode: "71151",
            street: "Testing avenue",
            city: "San Fransisco",
            state: "California",
            email: "john@doe.com",
            image: ""

        };

        waitsFor(function() {
            return apiCall == 3 ;
        }) ;
        runs(function() {


            fs.stat('index.jpeg', function (err, stats) {
                console.log(stats);
                console.info("user register started");

                restler.post(baseUrl + 'user/register', {
                    multipart: true,
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest'
                    },
                    data: {
                        token: _token,
                        phone: phone,
                        first_name: userInfo.firstName,
                        last_name: userInfo.lastName,
                        description: userInfo.description,
                        zip_code: userInfo.zipCode,
                        street: userInfo.street,
                        city: userInfo.city,
                        state: userInfo.state,
                        email: userInfo.email,
                        device_os: "ios",
                        device_token: "asdfas7sad6f899998j",
                        "folder_id": "0",
                        "image": restler.file("index.jpeg", null, stats.size, null, "image/jpeg")
                    }
                }).on("success", function (data) {
                    success = 1;
                    console.log(colors.blue("Success of user register"));

                    console.log(data);

                }).on("complete", function (data) {
                    if (!success)
                        console.log(colors.red(util.inspect(data)));
                });

            });

        });
    });

});