茉莉花测试不起作用,在期待间谍时得到不确定

时间:2016-11-08 20:18:18

标签: jasmine jasmine2.0

我正在尝试使用间谍来观看functino调用。但我无法使用我的代码。

我的测试

            it("calls the totalBogof() function", function() {
                spyOn(basket(), "totalBogof");
                basket().total();
                expect(basket.totalBogof).toHaveBeenCalled();
            });

我正在尝试测试的代码:

basket.prototype = {
    total: function(){
       var total=0.00;
       for(var i=0; i<shoppingBasket.length; i++){
            total += shoppingBasket[i].price;  
        }
         $('#total').html('total = '+currency+total.toFixed(2));
         this.totalBogof(total);
    },
    totalBogof: function(total){
         totalMinBOGOF = total - this.calcBOGOF();
         $('#total-bogof').html('Total - BOGOF = ' + currency + totalMinBOGOF.toFixed(2));
         this.totalPercentageDiscount(totalMinBOGOF, 10);
    },
}

我得到的错误是:

5 specs, 1 failureSpec List | Failures
shoppingBasket calls the totalBogof() function
Error: Expected a spy, but got undefined.
Error: Expected a spy, but got undefined.
 at compare     (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:3083:17)
 at Expectation.toHaveBeenCalled     (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:1480:35)
 at Object.<anonymous> (file:///C:/Users/G/shoppingbasket-jasmine/shopping-basket.js:56:47)
 at attemptSync     (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:1886:24)
 at QueueRunner.run     (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:1874:9)
 at QueueRunner.execute (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:1859:10)
 at Spec.queueRunnerFactory (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:697:35)
 at Spec.execute (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:359:10)
 at Object.fn (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:2479:37)
 at attemptAsync (https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js:1916:24)

我需要做些什么才能解决这个问题?

1 个答案:

答案 0 :(得分:2)

你尝试过这个吗?看看间谍documentation

End Sub