简体   繁体   English

单元测试无效方法-Jasmine,JavaScript

[英]Unit testing void methods - Jasmine, javascript

This is my function in the controller code that I have exposed. 这是我公开的控制器代码中的功能。

//unit test code
    $.getMessagesUnit = function(chatId) {

        if (!(Titanium.Network.networkType === Titanium.Network.NETWORK_NONE)) {

            apiHelper.APIGetRequest(xxxxxxxx + '/messages/' + args.chatId, function(e) {
                $.view_indicator.visible = true;
                var status = this.status;
                if (status == 200) {

                    console.log('get chats');

                    var data = JSON.parse(this.responseText);
                    console.log(data);

                    var rows = [];
                    var i = 0;
                    var blob;

                    for (x in data.messages) {
                        //Ti.API.debug(JSON.stringify(messages[x]));

                        var nickname = null;
                        var chatBlob = null;
                        var picture = null;
                        var timeStampCon = null;

                        chatBlob = data.messages[x].message;
                        nickname = data.messages[x].user.nickname;
                        picture = data.messages[x].user.pictures[0];

                        //alert(data.messages[x].message);

                        timeStampCon = utilities.getCurrentTimeFromStamp(data.messages[x].timestamp);
                        textArea.addLabel(timeStampCon);

                        //alert(data.messages[x].user.id);

                        if (args.pickedId == data.messages[x].user.id) {

                            textArea.recieveMessage(chatBlob);
                            textArea.addLabel("\n");

                        } else {

                            textArea.sendMessage(chatBlob);
                            textArea.addLabel("\n");

                        }

                    }

                    //return rows;
                    //end
                    $.view_indicator.visible = false;

                }


            }, function(err) {

                alert('Unknown error from api');
                $.view_indicator.visible = false;
            });

        } else {
            alert('No internet connection found');
            $.view_indicator.visible = false;
        }

    };

    //end

Here is my unit test code: 这是我的单元测试代码:

it('test chat window function.', function() {
    $ = Alloy.createController('chatWindow', {});
    expect($.getMessagesUnit(123).chatBlob). toBeDefined();
});

I want to test if the variables in the JSON request are defined, after the api call is made: 我想测试在api调用之后是否定义了JSON请求中的变量:

var nickname = null;
var chatBlob = null;
var picture = null;
var timeStampCon = null;

to ensure that the data I am getting back from the unit test is defined. 以确保定义了从单元测试中获取的数据。 I am getting the following error: 我收到以下错误:

[INFO] :   .  ============================================================
[ERROR] :  .  THERE WERE FAILURES!
[ERROR] :  .  ============================================================
[ERROR] :  .  Recap of failing specs:
[ERROR] :  .  ------------------------------------------------------------
[ERROR] :  .  chatWindow controller test chat window function.. - Expected undefined to be defined.

I am quite new to unit testing, how can I test individual variables in a void method like that? 我对单元测试还很陌生,如何在这样的void方法中测试单个变量?

Cheers guys 欢呼的家伙

Update - using spies: 更新-使用间谍:

it('test spies in receive message function.', function() {
    $ = Alloy.createController('chatWindow', {});
    spyOn($, 'textarea.recieveMessage');
    $.getMessagesUnit(123);
    expect(textarea.recieveMessage).toHaveBeenCalled();
});

[INFO] :   .   - test spies in receive message function.. (FAILED)
[INFO] :   .   - - Expected spy recieveMessage to have been called.

You can't get access to the chatBlob as its only accessible inside the closure created by your success function. 您无法访问chatBlob,因为只能在成功函数创建的闭包内部对其进行访问。 What you could do is add a spy to the recieveMessage function on your textArea object before you make a call to $.getMessagesUnit(123) then you check the spy for received calls and the args past to that call ie your chatBlob value. 您可以做的是在对$.getMessagesUnit(123)进行调用之前,在textArea对象上的recieveMessage函数中添加一个间谍,然后检查该间谍是否有已收到的调用以及该调用过去的参数,即您的chatBlob值。

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

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