gpt4 book ai didi

javascript - 单元测试 void 方法 - Jasmine、javascript

转载 作者:行者123 更新时间:2023-11-28 08:06:54 27 4
gpt4 key购买 nike

这是我公开的 Controller 代码中的函数。

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

这是我的单元测试代码:

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

我想测试在进行 api 调用后 JSON 请求中的变量是否已定义:

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

确保我从单元测试中返回的数据已被定义。我收到以下错误:

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

我对单元测试很陌生,如何在这样的 void 方法中测试单个变量?

大家干杯

更新 - 使用 spy :

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.

最佳答案

您无法访问 chatBlob,因为它只能在 success 函数创建的闭包内访问。您可以做的是在调用 $.getMessagesUnit(123) 之前向 textArea 对象上的 recieveMessage 函数添加一个 spy 您检查 spy 是否收到了调用以及该调用过去的参数,即您的 chatBlob 值。

关于javascript - 单元测试 void 方法 - Jasmine、javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24708373/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com