Can';t为模块模式编写Jasmine测试

Can';t为模块模式编写Jasmine测试,jasmine,Jasmine,我试图使用Jasmine测试一个简单的JavaScript类,但我无法确定测试失败的原因 // Define the class to be tested function Quiz() { // private variables var score = 1; function getScore() { return score; } // public methods

我试图使用Jasmine测试一个简单的JavaScript类,但我无法确定测试失败的原因

    // Define the class to be tested

    function Quiz() {
        // private variables
        var score = 1;

        function getScore() {
            return score;
        }

        // public methods
        return {
            getScore: getScore
        };
    }
在运行测试之前,请验证是否可以调用getScore方法

    // Get the score outside of the Jasmine test
    var myQuiz = new Quiz();
    var myScore = myQuiz.getScore();
    console.log("myScore = ", myScore);
现在试着运行测试

    describe("A Quiz", function() {
        it("should have a default score of 1", function() {
            // Get the score inside of the Jasmine test
            var quiz = new Quiz();
            console.log("Quiz object: ", quiz);
            var score = quiz.getScore();
            console.log("score", score);
            expect(score).toBe(1);
        });

    });
下面是运行测试的输出

A Quiz should have a default score of 1.
    ✘ TypeError: quiz.getScore is not a function in http://localhost:7357/scripts/QuizTest.js (line 26)
        @http://localhost:7357/scripts/QuizTest.js:26
myScore =  1
Quiz object:  {"score":0}
在运行测试之前,我验证getScore方法是可调用的。输出中的最后一行来自测试前的console.log语句。这将验证是否可以找到getScore

但是,如果我尝试在Jasmine测试中调用getScore方法,则找不到它。记录测验对象将验证只有分数变量而没有方法可见

Why can't Jasmine see the method?

他不认为函数getScore是因为您在quick()中,您可以使用参数或全局变量进行求解,也可以返回quick函数()+1 score,或者在一个var参数中声明它,传递或提供一个全局分数