简体   繁体   English

使用Restler进行茉莉E2E测试中的回调

[英]callbacks in jasmine E2E testing using restler

I am testing restful api's using restler and jasmine nodejs modules . 我正在使用restler和jasmine nodejs模块测试Restful api的功能。 I have created test files to test a single api call and test group of api calls that need input from each other . 我已经创建了测试文件来测试单个api调用和需要彼此输入的api调用测试组。 How can I provide the feedback / output from one such api call to another api call using proper jasmine describe - it - expect block format . 如何使用正确的茉莉花“描述-期望”块格式提供从这样一个api调用到另一个api调用的反馈/输出。 If I try put verifyOtp function in another it-block after getPhoneOtp then it don't execute after success of getting the OTP and thus fails . 如果我尝试将verifyOtp函数放在getPhoneOtp之后的另一个it-block中,那么在成功获得OTP后它不会执行,因此会失败。 Right now I am reading the outputs in the console only and not able to use jasmine expect function since I am not able to include verifyOtp function in a it-block . 现在,我仅在控制台中读取输出,并且不能使用jasmine Expect函数,因为我无法在it-block中包括verifyOtp函数。 Any suggestions would be appreciated . 任何建议,将不胜感激 。 Here is the code . 这是代码。

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

});

I read a few answers on stack overflow about asynchronous testing in jasmine and was able to write the code with the help of that . 我读了一些有关茉莉花异步测试的堆栈溢出问题的答案,并能够借助它编写代码。 Here is the code . 这是代码。

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

            });

        });
    });

});

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

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