1

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)

これを修正するために私がする必要があるアイデアはありますか?

4

1 に答える 1

3

これを試しましたか?スパイのドキュメントを見てください

it("calls the totalBogof() function", function() {
   spyOn(basket, "totalBogof");
   basket.total();
   expect(basket.totalBogof).toHaveBeenCalled();
});
于 2016-11-08T20:30:40.580 に答える