2017-09-12 13 views
0

これは私を狂わせてしまいます。どんな助けもありがとう。状況:JavaScript関数。ローカルでは動作しますが、Lambda Minibootcampでは動作しません。

既存のオブジェクトであるstoreItemには、2つの関連するプロパティpriceおよびdiscountPercentageがあります。私はaddCalculateDiscountPriceという関数を(オブジェクトの外側から)書き、calculateDiscountPriceというメソッドを追加して割引価格を返すstoreItemを作成します。ここで

は、コードは次のとおりです。

function addCalculateDiscountPriceMethod(storeItem) { 
    // add a method to the storeItem object called 'calculateDiscountPrice' 
    // this method should multiply the storeItem's 'price' and 'discountPercentage' to get the discount 
    // the method then subtracts the discount from the price and returns the discounted price 
    // example: 
    // price -> 20 
    // discountPercentage -> .2 
    // discountPrice = 20 - (20 * .2) 
    storeItem.calculateDiscountPrice = function() { 
    var discount = this.discountPercentage; 
    var saved = this.price * discount; 
    var finalPrice = this.price - saved; 
    return finalPrice; 
    }; 
} 

これは私がのgitでそれをクローニングした後、各割り当てディレクトリ内NPMをインストールしているラムダJavaScriptのミニ合宿の一部です。 jsnodeを使ってこの同じコードを実行すると、コメント内の変数例を使用すると、16の期待される出力が得られます。私はこのコードにこのパスNPMのテストをしようと何度も書き直されている

FAIL tests/test.js 
    ● addCalculateDiscountPriceMethod(storeItem) › should add the method 'calculateDiscountPrice' to the store item object 

    TypeError: Cannot read property 'calculateDiscountPrice' of undefined 

    at Object.<anonymous> (tests/test.js:209:64) 

    ● addCalculateDiscountPriceMethod(storeItem) › should return the discount price from the new 'calculateDiscountPrice' method 

:私はNPMテストを実行したときしかし、私は、次のエラーを取得します。私はブラケット記法を使用して、明らかに単純な計算を1行(返品価格 - (price * discountPercentage))に戻してみました。元の試行に加えて、両方の再試行はライブ端末でうまくいきましたnodejsセッション。

なぜnpmテストではうまくいかないのですか?私は何を見ていないのですか?

アップデート:ここでは、NPMのテストファイルから該当するテストコードです:

describe('addCalculateDiscountPriceMethod(storeItem)', function() { 
    var storeItem = { 
    price: 80, 
    discountPercentage: 0.1 
    }; 
    var storeItem2 = { 
    price: 5, 
    discountPercentage: 0.5 
    }; 

    it('should add the method \'calculateDiscountPrice\' to the store item object', function() { 
    expect(exercises.addCalculateDiscountPriceMethod(storeItem).calculateDiscountPrice).toBeDefined(); 
expect(exercises.addCalculateDiscountPriceMethod(storeItem2).calculateDiscount Price).toBeDefined(); 
    }); 
    it('should return the discount price from the new \'calculateDiscountPrice\' method', function() { 
expect(exercises.addCalculateDiscountPriceMethod(storeItem).calculateDiscountPrice()).toBe(72); 
expect(exercises.addCalculateDiscountPriceMethod(storeItem2).calculateDiscountPrice()).toBe(2.5); 
    }); 
}); 

答えて

1

出力はすでにそれが関数の引数であり、あなたが前にそれで何もしないされているのでstoreItemは、未定義であることを語っています関数を追加しようとすると、問題はテストそのものでなければなりません。

テストファイルを見て、引数が関数に渡されているかどうかを確認します。または使用する

関数内では、argumentsは関数呼び出しに渡されるパラメータを含む予約語です。

編集:あなたは、それはそれゆえあなたがaddCalculateDiscountPriceMethodでstoreItemを返さなければなりません未定義にcalculateDisconutPriceを呼んでいるものを返すされていないので、この2つの呼び出し

addCalculateDiscountPriceMethod(storeItem) 
    .calculateDiscountPrice() 

を連鎖されたテストを見ています。

+0

Isidro上記のテストコードをご覧ください。 –

+1

ソリューションで編集してください! –

+0

Insidro私はあなたの説明を理解しますが、これを修正するために正確に何を書いていますか? –

関連する問題