Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何跟踪使用特定参数调用函数的次数_Javascript_Jasmine - Fatal编程技术网

Javascript 如何跟踪使用特定参数调用函数的次数

Javascript 如何跟踪使用特定参数调用函数的次数,javascript,jasmine,Javascript,Jasmine,我试图建立一个jasmine测试,而不仅仅是跟踪函数被调用的次数。我想跟踪使用某个参数调用函数的次数 以下是我当前的测试规范: describe('fizzBuzz', function(){ beforeEach(function(){ // console.log = jasmine.createSpy('log'); spyOn(console, 'log'); fizzBuzz();

我试图建立一个jasmine测试,而不仅仅是跟踪函数被调用的次数。我想跟踪使用某个参数调用函数的次数

以下是我当前的测试规范:

describe('fizzBuzz', function(){
        beforeEach(function(){
            // console.log = jasmine.createSpy('log');
            spyOn(console, 'log');
            fizzBuzz();
        })

        it("should test for the fizzBuzz's console output", function () {
        expect(console.log).toHaveBeenCalledWith('FizzBuzz');
        });
        it('tracks how many times FizzBuzz is called', function(){
            expect(console.log.callCount).toEqual(6);
        });
    })
在我的第二个规范中,callCount等于调用console.log的次数。我想追踪它被称为“FizzBuzz”的次数。有没有办法把我的第一个规范和第二个规范联系起来?我尝试了很多变化,但都没有找到解决方案

下面是我要测试的代码:

var fizzBuzz = function(){
    var start = 1;
    while(start <= 100){
        if(start % 3 === 0 && start % 5 === 0){
            console.log('FizzBuzz');
        } else if (start % 3 === 0){
            console.log('Fizz');
        } else if (start % 5 === 0){
            console.log('Buzz');
        } else {
            console.log(start);
        }
        start++;
    }
};
var fizzBuzz=function(){
var start=1;
while(start
var fizzBuzz=function(){
var start=1;

虽然(首先,我喜欢这种方法,我认为这是可行的。不幸的是,我一直得到一个“undefined”类型错误:undefined在我的测试规范中不是一个函数。我在其他尝试中也遇到过这个错误,我不确定undefined指的是什么。我刚刚对我的代码所做的更新对我来说是可行的。这正是我在我的测试规范中得到的因此,如果您仍然收到该错误,则会出现其他错误。您的spec脚本上是否运行了其他函数?我不确定。我复制并粘贴了上面的代码,并在两个单独的js文件中进行了尝试。我不断收到“TypeError:undefined不是函数”我正在尝试找出代码中的“undefined”部分。是否继续您的spec.js或specRunner.html中都包含jQuery?
Console.log()
是一个jQuery函数,因此如果您没有,这可能是您的问题。我目前确实有jQuery。它是Testem()的依赖项,我使用它来运行测试。
fizzBuzz tracks how many times console.log('FizzBuzz') is called.
    ✘ TypeError: undefined is not a function
        at null.<anonymous> (http://localhost:7357/EJSLoops.js:31:43)
        at jasmine.Block.execute (http://localhost:7357/testem/jasmine.js:1064:17)
        at jasmine.Queue.next_ (http://localhost:7357/testem/jasmine.js:2096:31)
        at jasmine.Queue.start (http://localhost:7357/testem/jasmine.js:2049:8)
        at jasmine.Spec.execute (http://localhost:7357/testem/jasmine.js:2376:14)
        at jasmine.Queue.next_ (http://localhost:7357/testem/jasmine.js:2096:31)
        at onComplete (http://localhost:7357/testem/jasmine.js:2092:18)
        at jasmine.Spec.finish (http://localhost:7357/testem/jasmine.js:2350:5)
        at null.onComplete (http://localhost:7357/testem/jasmine.js:2377:10)
        at jasmine.Queue.next_ (http://localhost:7357/testem/jasmine.js:2106:14)
var fizzBuzz = function(){
    var start = 1;
    while(start <= 100){
        if(start % 3 === 0 && start % 5 === 0){
            console.log('FizzBuzz');
        } else if (start % 3 === 0){
            console.log('Fizz');
        } else if (start % 5 === 0){
            console.log('Buzz');
        } else {
            console.log(start);
        }
        start++;
    }
};

describe('fizzBuzz', function(){
    beforeEach(function(){
        // console.log = jasmine.createSpy('log');
        spyOn(console, 'log');
        fizzBuzz();
    });

    it("should test for the fizzBuzz's console output", function () {
        expect(console.log).toHaveBeenCalledWith('FizzBuzz');
    });

    it("tracks how many times console.log('FizzBuzz') is called", function(){

        var callcount = console.log.calls.count();
        var paramcount = 0;
        for(var i = 0; i < callcount; i++){
            if(console.log.calls.argsFor(i) == 'FizzBuzz'){
                paramcount++;
            }
        }
        expect(paramcount).toEqual(6);

    });
});