简体   繁体   中英

Mocha delivering “not a function response” in testing

Just started using mocha as a testing suite, and coming back up to speed on javascript after a few years of non-use, so this may or may not be a dumb question with an obvious answer. Just testing stuff out and seeing what works.

I have a small file of calculation functions in a file called index.js, which is the file mocha is set to jump off.

function add(numA, numB) {
    return parseFloat(numA) + parseFloat(numB);
}

function subtract(numA, numB) {
    return parseFloat(numA) - parseFloat(numB);
}

function divide(numA, numB) {
    return parseFloat(numA) / parseFloat(numB);
}

function multiply(numA, numB) {
    return parseFloat(numA) * parseFloat(numB);
}

function modulo(numA, numB) {
    return parseFloat(numA) % parseFloat(numB);
}

Tried to keep it as simple as could be while allowing some variety and testing room. Anyway, I then attempted to write a set of test cases for it, also simple just mocha test cases, no special assert module or anything, purely simple mocha and javascript.

describe("index", function() {
    var assert = require("assert");
    var x = 12;
    var y = 34;
    var index = require('../app/index');

    beforeEach(function() {
        x++;        
        y--;
        console.log("   x = " + x + "; y = " + y);
    });
    describe("index", function() {
        describe("Addition", function() {
            it("should add numbers", function() {
                assert.equal(x + y, index.add(x, y));
            });
        });
        describe("Subtraction", function() {
            it("should subtract numbers", function() {
                assert.equal(x - y, index.subtract(x, y));
            });
        });
        describe("Division", function() {
            it("should divide numbers", function() {
                assert.equal(x / y, index.divide(x, y));
            });
        });
        describe("Multiplication", function() {
            it("should multiply numbers", function() {
                assert.equal(x * y, index.multiply(x, y));
            });
        });
        describe("Modulus", function() {
            it("should modulo numbers", function() {
                assert.equal(x % y, index.modulo(x, y));
            });
        });
    });
});

Basically just wrote these to see if I could get mocha to work in the way I thought it should be working, because the examples I saw all made sense. However, when I ran this code, it ended up not working, with the reason that any function I attempted to call with index failed, index.add, index.subtract, and so on, specifically because of TypeError: index.[function_name] is not a function.

From what I've read and seen in other people's code, this should be working. My folders are setup such that the file I'm testing is in \\lscmocha\\app and the test file these tests are contained in is in \\lscmocha\\test. Any help would be appreciated, sorry if it's an obvious answer or a duplicate question, I looked for a while and couldn't find anything.

Sunil D. asked if I had exported my functions, and I had not. Must have skipped the section that told me to. If you're having the same trouble as me, just make sure your functions are exported within your main javascript script files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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