使用 Jasmine 访问 js 模块的方法,以便可以单独测试它们

Access methods of js module with Jasmine so they can be individually tested

我正在用 Jasmine 做一些单元测试。我有一个模块 getContacts,它使用 module.exportsrequire 成功注入到规范中。虽然我可以访问模块本身,但模块方法返回未定义,所以我无法将它们作为单元进行测试。以下是模块的代码:

var getContacts = function() {

    var GetContacts = this;
    var contacts = require('nativescript-contacts');
    var model = require("../main-view-model");

    GetContacts.init = (function() {
        var self = this;
        contacts.getContact().then(function(args){
            self.makeName(args.data);
            self.getPhoneNumber(args.data);
        }).catch(function(e) {
            console.log("promise \"contacts.getContact()\" failed with" + e.stack + "\n" + "value of self:" + " " + self)
        });
    }());

    GetContacts.getPhoneNumber = function(data) {
        if(data.phoneNumbers.length > 0){
            model.phone = data.phoneNumbers[0];
        }
    };

    GetContacts.makeName = function(data) {
        if(data.name.displayname) {
            model.contactName = data.name.displayname;
        }
        else {

        }
        model.contactName = data.name.given + " " + data.name.family;
    };
};

module.exports = getContacts;

和规范文件:

describe("getContacts", function() {
    "use strict";

    var contacts, model, getContacts, data;

    beforeEach(function() {
        contacts = require('nativescript-contacts');
        model = require("../main-view-model");
        getContacts = require('../src/getContacts.js');

        data = {
            "data": {
                "name": {
                    "given": "John",
                    "middle": "Peter",
                    "family": "Smith",
                    "prefix": "Mr.",
                    "suffix": "Jr.",
                    "testEmptyObject": {},
                    "testEmptyString": "",
                    "testNumber": 0,
                    "testNull": null,
                    "testBool": true,
                    "displayname": "John Smith",
                    "phonetic": {
                        "given": null,
                        "middle": null,
                        "family": null
                    }
                }
            },
            "response": "selected"
        }
    });

    it("Gets the display name as contact name if display name is a string with length", function() {
        expect(getContacts.makeName(data)).toBe("John Smith");
    });

});

测试失败并出现错误:

getContacts.makeName is not a function

并且确实记录了它 returns undefined。然而,记录 getContacts 会将整个 getContacts 函数打印到控制台。如何访问 makeName 和其他方法以便我可以在个人级别对它们进行单元测试?

function 不 return 任何东西,所以 function 会做一些事情,但不知道对它内部代码的进一步引用,所以尝试在函数末尾添加 return ,如下所示 ;)

var getContacts = function() {

    var GetContacts = this;

    /*
    rest of code
    */

    GetContacts.makeName = function(data) {
        if(data.name.displayname) {
            model.contactName = data.name.displayname;
        }
        else {
        }
        model.contactName = data.name.given + " " + data.name.family;
    };

    //add return to export content of function
    return GetContacts;
};

需要在没有包装匿名函数的情况下重构代码。我想不需要它,因为模块本身会产生分离。这是有效的代码:

var contacts = require('nativescript-contacts'); var model = require("../main-view-model");

var GetContacts = {

    init: function() {
        var self = this;
        contacts.getContact().then(function(args){
            model.contactName = self.makeName(args.data);
            model.phone = self.getPhoneNumber(args.data);
        }).catch(function(e) {
            console.log("promise \"contacts.getContact()\" failed with" + e.stack + "\n" + "value of self:" + " " + self)
        });
    },

    getPhoneNumber: function(data) {
        if(data.phoneNumbers.length > 0){
            return data.phoneNumbers[0];
        }
    },

    makeName: function(data) {
        if(data.name.displayname) {
            return data.name.displayname;
        }
        else {
        }
    }

};

module.exports = GetContacts;