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

时间:2014-06-13 17:29:57

标签: javascript 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的次数。我想通过参数' FizzBu​​zz'来跟踪它被调用了多少次。有没有办法将我的第一个规格链接到我的第二个?我尝试了很多变化,无法找到解决方案。

以下是我要测试的代码:

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

我正在使用console.log参数来确定代码是否正确。例如:如果使用参数FizzBu​​zz调用console.log 6次中的6次,则他们的代码将遵守指南。

更新:

来自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)

1 个答案:

答案 0 :(得分:2)

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

    });
});