2017-03-05 4 views
0

私は簡単な方法でユニットテストを作り、if文が、私の予想結果は8でなければなりませんが、テストは、それはここで実際に7NodeJsユニットテスト、実際の結果が正しくない

であるというエラーを与えていますが、私のユニットテストです。

describe("Discount code 10% + 20% age", function() { 
     it("If code is abcd or efgh give 10% discount and if age is lower than 15 or higher than 65 plus 20% discount ", function() { 
      // Hier worden variabelen gekopeld aan de returns van de functies 
      var testCaseDiscount1 = converter.calculateTotalPrice(66, "abcd"); 
      var testCaseDiscount2 = converter.calculateTotalPrice(15, "efgh"); 
      var testCaseDiscount3 = converter.calculateTotalPrice(64, "fffhfh"); 
      var testCaseDiscount4 = converter.calculateTotalPrice(20, "fdhdfhfd"); 
      var testCaseDiscount5 = converter.calculateTotalPrice(15, "notgoodcode"); 
      var testCaseDiscount6 = converter.calculateTotalPrice(67, "notgoodcode"); 

      // Hier worden de antwoorden vergeleken. 
      expect(testCaseDiscount1).to.equal(7); 
      expect(testCaseDiscount2).to.equal(7); 
      expect(testCaseDiscount3).to.equal(10); 
      expect(testCaseDiscount4).to.equal(10); 
      expect(testCaseDiscount5).to.equal(8); 
      expect(testCaseDiscount6).to.equal(8); 
     }); 
     }); 

私のif文:

exports.calculateTotalPrice = function(age, code) { 
    var price = 10; 
if (age >= 65 || age <= 15 && code == "abcd" || code == "efgh") { 
    var result = (price/100 * 30); 
    var price = (price - result); 
    return price; 

    } else if (age >= 65 || age <= 15 && code == "notgoodcode") { 
    var result = (price/100 * 20); 
    var price = (price - result); 
    return price; 

    } else { 
     return price; 
    } 
} 

と私の結果は次のようになります。

AssertionError: expected 7 to equal 8 
     + expected - actual 

     -7 
     +8 

     at Context.<anonymous> (test\TicketTest.js:50:38) 

"notgoodcode"の年齢15は過ぎますが、65歳以上の人は決して通過せず、2番目のif文ではなく最初のif文にリダイレクトされるので、これは奇妙だと思います。

ありがとうございます。

答えて

1

これは、演算子の優先順位に関連しています。 ||の前に&&が実行されます。期待される結果を得るには、かっこを追加する必要があります。

if ((age >= 65 || age <= 15) && code == "abcd" || code == "efgh") { 

} else if ((age >= 65 || age <= 15) && code == "notgoodcode") { 
+0

ありがとうございました^^ –

関連する問題