简体   繁体   English

摩卡在测试中传递“不是功能响应”

[英]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. 刚开始使用mocha作为测试套件,并且在不使用几年后又恢复使用javascript的速度,因此这可能是一个愚蠢的问题,而且可能没有明显的答案。 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. 我在一个名为index.js的文件中有一个小的计算函数文件,该文件是mocha设置为可跳转。

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. 无论如何,然后我尝试为其编写一组测试用例,也就是简单的mocha测试用例,没有特殊的assert模块或任何东西,纯粹是简单的mocha和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. 但是,当我运行此代码时,它最终无法正常工作,原因是我尝试使用索引调用的任何函数均失败,包括index.add,index.subtract等,尤其是由于TypeError:index。[function_name]不是功能。

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. 我的文件夹已设置为要测试的文件位于\\ lscmocha \\ app中,而包含这些测试的测试文件位于\\ 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. Sunil D.问我是否导出了函数,但没有。 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. 如果您遇到与我同样的麻烦,只需确保将函数导出到主要的JavaScript脚本文件中即可。

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

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