简体   繁体   English

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

[英]How to track how many times a function was called with a specific parameter

I am trying to set up a jasmine test to not just track how many times a function is called. 我试图设置一个茉莉花测试,以不仅仅跟踪调用一个函数的次数。 I would like to track how many times a function is called with a certain parameter. 我想追踪一个函数被某个参数调用多少次。

Here is my current test spec: 这是我当前的测试规格:

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);
        });
    })

In my second spec, the callCount is equal to how many times console.log is called. 在我的第二个规范中,callCount等于console.log被调用的次数。 I would like to track how many times it was called with the argument 'FizzBuzz'. 我想跟踪使用参数“ FizzBu​​zz”调用它的次数。 Is there a way to chain my first spec with my second? 有没有办法将我的第一个规格与我的第二个规格联系起来? I have tried a lot of variations and can't come to a solution. 我尝试了很多变体,无法解决。

Here is the code I would like to test: 这是我要测试的代码:

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++;
    }
};

I am using the console.log parameters to determine if the code is correct. 我正在使用console.log参数来确定代码是否正确。 Ex: If console.log is called 6 times out of 100 with the parameter FizzBuzz, their code would abide to the guidelines. 例如:如果使用参数FizzBu​​zz在console.log中调用了100次中的6次,则它们的代码将遵守准则。

UPDATE: 更新:

Current Error from Testem: 来自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);

    });
});

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

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