HelloWorld
HelloWorld

Reputation: 11257

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. I would like to track how many times it was called with the argument 'FizzBuzz'. 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. Ex: If console.log is called 6 times out of 100 with the parameter FizzBuzz, their code would abide to the guidelines.

UPDATE:

Current Error from 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)

Upvotes: 1

Views: 2114

Answers (1)

j_buckley
j_buckley

Reputation: 1936

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

    });
});

Upvotes: 2

Related Questions